Assignment 3

pdf

School

Northeastern University *

*We aren’t endorsed by this school

Course

6400

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

6

Uploaded by ColonelMosquitoMaster1017

Report
Assignment 3 If Statements: In [1]: In [2]: In [3]: In [4]: In [5]: In [6]: Largest Integer: 10 Enter the year:2024 Leap Year! Enter the string: Hello1 The string has atleast one uppercase letter, one lowercase letter and one digit. # 1 list_integers = [ 2 , 4 , 6 , 8 , 10 ] largest_integer = 0 for i in list_integers: if i >= largest_integer: largest_integer = i print ( "Largest Integer: " , largest_integer) # 2 year = int ( input ( "Enter the year:" )) if year % 4 == 0 : print ( "Leap Year!" ) else : print ( "Not a Leap Year!" ) # 3 input_string = input ( "Enter the string: " ) uc_flag = False lc_flag = False num_flag = False for i in input_string: if i.isupper(): uc_flag = True if i.islower(): lc_flag = True if i.isdigit(): num_flag = True if uc_flag and lc_flag and num_flag: print ( "The string has atleast one uppercase letter, one lowercase else : print ( "The string doesn't have the required specifications." ) KumariSimran_Assignment 3 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 1 of 6 9/28/23, 9:55 PM
In [7]: In [8]: Loops: In [9]: In [10]: In [11]: In [12]: In [13]: banana cherry Enter the number to calculate factorial: 5 Factorial of 5 is: 120 Enter the number of terms in Fibonacci Series: 10 Fibonacci Series till 10th term is: [0, 1, 1, 2, 3, 5, 8, 13, 21, 3 4] # 4 list_strings = [ 'apple' , 'banana' , 'cherry' , 'date' ] for i in list_strings: if len (i) > 5 : print (i) # 1 n = int ( input ( "Enter the number to calculate factorial: " )) i = 1 factorial = 1 while i <= n: factorial = factorial * i i = i + 1 print ( "Factorial of {} is: " .format(n),factorial) # 2 n = int ( input ( "Enter the number of terms in Fibonacci Series: " )) fibonacci = [ 0 , 1 ] term_1 = 0 term_2 = 1 for i in range ( 1 ,n - 1 ): term_3 = term_1 + term_2 fibonacci.append(term_3) term_1 = term_2 term_2 = term_3 print ( "Fibonacci Series till {}th term is: " .format(n),fibonacci) # 3 KumariSimran_Assignment 3 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 2 of 6 9/28/23, 9:55 PM
In [14]: In [15]: In [16]: Functions: In [17]: Prime Numbers between 1 and 50 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 Enter a String: ThisString Original String: ThisString Reverse String: gnirtSsihT def prime (n): if n == 1 : return True else : for i in range ( 2 ,n): if n % i == 0 : return False break else : return True def print_prime (start,stop): print ( "Prime Numbers between {} and {} are: " .format(start,stop)) for i in range (start,stop + 1 ): if prime(i): print (i) print_prime( 1 , 50 ) # 4 string = input ( "Enter a String: " ) reverse_str = '' for i in range ( len (string) - 1 , - 1 , - 1 ): reverse_str = reverse_str + string[i] print ( "Original String: {}" .format(string)) print ( "Reverse String: {}" .format(reverse_str)) # 1 KumariSimran_Assignment 3 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 3 of 6 9/28/23, 9:55 PM
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
In [18]: In [19]: In [20]: In [21]: [3, 4] Original Matrix: [1, 2, 3] [4, 5, 6] Transposed Matrix: [1, 4] [2, 5] [3, 6] def find_common_elements (list_1,list_2): common_elements = [] for i in list_1: if i in list_2: common_elements.append(i) return common_elements l1 = [ 1 , 2 , 3 , 4 ] l2 = [ 3 , 4 , 5 , 6 ] print (find_common_elements(l1,l2)) # 2 matrix = [[ 1 , 2 , 3 ],[ 4 , 5 , 6 ]] def matrix_transpose (matrix): transposed_matrix = [] rows = len (matrix[ 0 ]) column = len (matrix) for i in range (rows): row_temp = [] for j in range (column): row_temp.append(matrix[j][i]) transposed_matrix.append(row_temp) return transposed_matrix def print_matrix (matrix): for i in matrix: print (i) print ( "Original Matrix: " ) print_matrix(matrix) print ( "Transposed Matrix: " ) print_matrix(matrix_transpose(matrix)) # 3 KumariSimran_Assignment 3 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 4 of 6 9/28/23, 9:55 PM
In [22]: In [23]: In [24]: Lambda Expressions: In [25]: In [26]: In [27]: Enter String: Simran The count of the vowels in the input string is: 2 Enter the nth term in fibonacci series: 10 The 10th term of fibonacci series is: 34 [(1, 5), (3, 2), (2, 8)] [(3, 2), (1, 5), (2, 8)] input_string = input ( "Enter String: " ) def count_vowels (string): vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' ] list_string = list (string) count_vowel = 0 for i in list_string: if i in vowels: count_vowel = count_vowel + 1 return count_vowel print ( "The count of the vowels in the input string is: {}" .format(coun # 4 number = int ( input ( "Enter the nth term in fibonacci series: " )) def fibonacci (n): if n <= 1 : return n else : return fibonacci(n - 1 ) + fibonacci(n - 2 ) print ( "The {}th term of fibonacci series is: {}" .format(number,fibonac # 1 list_of_tuples = [( 1 , 5 ), ( 3 , 2 ), ( 2 , 8 )] print (list_of_tuples) list_of_tuples.sort(key = lambda x: x[ 1 ]) print (list_of_tuples) # 2 KumariSimran_Assignment 3 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 5 of 6 9/28/23, 9:55 PM
In [34]: In [29]: In [30]: In [31]: In [32]: Original list of integers: [22, 25, 35, 36, 39, 50, 52, 55, 57, 64] Even numbers from the said list: [22, 36, 50, 52, 64] {'value': 15} Enter the length of the list: 5 Enter the list of numbers : 4 5 6 7 8 [16.0, 25.0, 36.0, 49.0, 64.0] nums = [ 22 , 25 , 35 , 36 , 39 , 50 , 52 , 55 , 57 , 64 ] print ( "Original list of integers:" ) print (nums) print ( "\nEven numbers from the said list:" ) even_nums = list ( filter ( lambda x: x % 2 == 0 , nums)) print (even_nums) # 3 dictionary = [{ 'value' : 10 }, { 'value' : 5 }, { 'value' : 15 }] max_value = max (dictionary, key = lambda x: x[ 'value' ]) print (max_value) # 4 list_len = int ( input ( "Enter the length of the list: " )) numbers = list ( map ( float , input ( "Enter the list of numbers : " ).strip( result = map ( lambda x: x * x, numbers) print ( list (result)) KumariSimran_Assignment 3 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 6 of 6 9/28/23, 9:55 PM
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help