387
Pe
python-exercices / files
python-exercices / level_one / ex_all.py
3.4 KB
1# Question 1 2# Level 1 3 4# Question: 5# Write a program which will find all such numbers which are divisible by 7 but 6# are not a multiple of 5, between 2000 and 3200 (both included). 7# The numbers obtained should be printed in a comma-separated sequence on a 8# single line. 9 10 11def divisibleBySeven(): 12 result = [] 13 for i in range(2000, 3200): 14 if (i % 7 == 0) and (i % 5 != 0): 15 result.append(str(i)) 16 17 return ','.join(result) 18 19 20# Question 2 21# Level 1 22 23# Question: 24# Write a program which can compute the factorial of a given numbers. 25# The results should be printed in a comma-separated sequence on a single line. 26# Suppose the following input is supplied to the program: 27# 8 28# Then, the output should be: 29# 40320 30 31 32def factorialOfNumber(): 33 number = int(input('\n\nEnter a positive number\n')) 34 35 def calculateFactorial(n): 36 if n == 0: 37 return 1 # Factorial of 0 is 1. 38 return n * calculateFactorial(n - 1) 39 40 return calculateFactorial(number) 41 42 43# Question 3 44# Level 1 45 46# Question: 47# With a given integral number n, write a program to generate a dictionary that 48# contains (i, i*i) such that is an integral number between 1 and n (both 49# included). and then the program should print the dictionary. 50# Suppose the following input is supplied to the program: 51# 8 52# Then, the output should be: 53# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} 54 55# Hints: 56# In case of input data being supplied to the question, it should be assumed to 57# be a console input. Consider use dict() 58 59def powerDictionaryFromNumber(): 60 number = int(input('\n\nEnter a positive number\n')) 61 62 def powerDictionary(number): 63 result = {} 64 for i in range(1, number+1): 65 result = {**result, i: i*i} 66 67 return result 68 69 return powerDictionary(number) 70 71 72# Question 4 73# Level 1 74 75# Question: 76# Write a program which accepts a sequence of comma-separated numbers from 77# console and generate a list and a tuple which contains every number. 78# Suppose the following input is supplied to the program: 79# 34,67,55,33,12,98 80# Then, the output should be: 81# ['34', '67', '55', '33', '12', '98'] 82# ('34', '67', '55', '33', '12', '98') 83 84# Hints: 85# In case of input data being supplied to the question, it should be assumed to 86# be a console input. tuple() method can convert list to tuple 87 88 89def generateListsFromInput(): 90 numbers_string = input('\n\nEnter a comma separated list of numbers\n') 91 numbers_list = numbers_string.split(',') 92 numbers_tuple = tuple(numbers_list) 93 return numbers_list, numbers_tuple 94 95 96# Question 5 97# Level 1 98 99# Question: 100# Define a class which has at least two methods: 101# getString: to get a string from console input 102# printString: to print the string in upper case. 103# Also please include simple test function to test the class methods. 104 105# Hints: 106# Use __init__ method to construct some parameters 107 108 109class StringManipulator: 110 111 def __init__(self): 112 self.current_string = '' 113 114 def getString(self): 115 self.current_string = input('\n\nEnter a string\n') 116 117 def printString(self): 118 print(self.current_string.upper()) 119 120 121def stringManipulatorTest(): 122 subject = StringManipulator() 123 subject.getString() 124 subject.printString() 125 126 127def main(): 128 print(divisibleBySeven()) 129 print(factorialOfNumber()) 130 print(powerDictionaryFromNumber()) 131 132 results = generateListsFromInput() 133 print(results[0]) 134 print(results[1]) 135 136 stringManipulatorTest() 137 138 139if __name__ == '__main__': 140 main()