Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exercise 11.1 – Iteration
- # a. Write a program that will print your full name and student number 10 times.
- for i in range(10):
- print("My name is Rohit Biswas")
- print("My student number is 001811301033")
- # b. Write another program that will ask the user for a “wish” (eg. What do you wish for on
- # your birthday) and the number of time they want that wish to be displayed. Then print the
- # wish the number of times specified.
- wish = input("What do you wish for on your birthday: ")
- num = int(input("How many times do you want wish to be displayed: "))
- for j in range(num):
- print(wish)
- # Exercise 11.2 – Sum
- # Given numLst = [1, 2, 3, 4, 5] we can compute the sumImperative in an imperative way. You have
- # to implement it such that it uses imperative way to compute the sum of the list.
- numLst = [1, 2, 3, 4, 5]
- sumImperative = 0
- for n in numLst:
- sumImperative += n
- print(sumImperative)
- # Exercise 11.3 – Even numbers
- # Using the imperative way of programming, find even numbers (printEvenNumbers) given a list of
- # natural numbers:
- naturalNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- for i in naturalNumbers:
- if i % 2 == 0:
- print(i)
- # Exercise 11.4 – Lists
- # Using the imperative coding style, implement a function groupList that given a list (list) and a
- # group length (glength), returns a list of lists with length glength.
- def groupList(l, s):
- main_output = []
- temp_list = []
- n = 0
- for i in l:
- temp_list.append(i)
- n = n + 1
- if n == s:
- main_output.append(temp_list.copy())
- temp_list = []
- n = 0
- return main_output
- #ab = [1,2,3,4,5,6,7,8,9]
- #print(groupList(ab,3))
- # Exercise 11.5 – Character Lists
- # Write a program to check that a given character is in a given string. It will ask the user to enter a
- # string and a character to search. Use linear search for this program where you loop through all the
- # characters.
- st = input("Enter a string: ")
- ch = input("Enter a character to search: ")
- found = False
- for c in st:
- if c == ch:
- found = True
- if found:
- print("The character is present in the string")
- else:
- print("The character is not found in the string")
- # Exercise 13.1
- # Given numLst = [1, 2, 3, 4, 5], define a functional sumFunctional to sum the list.
- def sumFunctional(l):
- sum = 0
- for i in l:
- sum += i
- return sum
- numLst = [1, 2, 3, 4, 5]
- print(sumFunctional(numLst))
- # Exercise 13.2 – Recursion
- # Recall from F# sessions, where the factorial of an integer number is defined by the following
- # a. Write a Python factorialR() function using recursion
- def factorialR(n):
- if n == 0 or n == 1:
- return 1
- else:
- return n * factorialR(n-1)
- #print(factorialR(5))
- # b. Write a Python factorialI() function using iterative style:
- def factorialI(n):
- factorial = 1
- if n > 0:
- for i in range(1, n+1):
- factorial = factorial * i
- return factorial
- #print(factorialI(5))
- # Exercise 13.3 – Higher-order, lambda
- # Write a function to find even numbers (printEvenNumbers) given a list of natural numbers:
- # naturalNumbers = [0,1,2,3,4,5,6,7,8,9]
- # Use lambda and filter() function.
- naturalNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- def printEvenNumbers(l):
- even = list(filter(lambda i: (i % 2 == 0), l))
- print("Even numbers in the list: ", even)
- printEvenNumbers(naturalNumbers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement