# # For loop in strings and lists practice - Practice # # Part I - Scanning values and repetition # # Preliminary notes: - Presumed knowledge: - That practiced in pervious videos - Basic structure of strings, lists - for loop - The functions range(), length(), eval() - The operators in, *, + - Relation to tuples - Use only the studied material. In particular do not use type functions # INTRODUCTORY REMARK ABOUT THE NATURE OF # THE FOR LOOP AND ABOUT ITS KINDS 98, 73, 52, 61 0 1 2 3 # NOW TO THE QUESTIONS # SCANNING VALUES # Q. 1 # Write a program that receives any string # and prints all its characters, # one by one, each in a different line. s = input("Please enter any string: ") for c in s: print(c) Python c = 'P' c = 'y' c = 't' c = 'h' c = 'o' c = 'n' # Q. 2 # Write a program that receives any string # and prints its length. Do not use the function # length(). s = input("Please enter any string: ") counter = 0 for c in s: counter = counter + 1 print(counter) 'Python' # Q. 3 # Write a program that receives an non-negative integer # and prints the sum of digits comprising it. num = input("Please enter any non-negative integer: ") sumNums = 0 for digit in num: sumNums = sumNums + int(digit) print(sumNums) 972 # Q. 4 # Write a program that receives an integer non-negative i # and a string s, and prints the character # found in index i in s. If i is not a valid # index in s the programm issues a proper message. # Do not use the [ ] operator s = input("Please enter any string: ") i = int(input("Please enter a non-netative integer: ")) if i <= (len(s) - 1): counter = 0 for c in s: if counter == i: print(c) counter = counter + 1 else: print("Invalid index.") s = "Bisli" 'B' 'i' 's' 'l' 'i' 0 1 2 3 4 # Q. 5 # Write a program that receives two integers, # i and j, as well as a string s. # The program prints the substring of s # beginning in the index i and ends # in the index j. Each character of the # substring is printed in a separate line. s = input("Please enter any string: ") i = int(input("Please enter a non-netative integer: ")) j = int(input("Please enter a non-netative integer: ")) counter = 0 for c in s: if counter >= i and counter <= j: print(c) counter = counter + 1 "Banana" 012345 Bamba 01234 # Q. 6 # Write a program that recieves any string # and a character (string of length 1), and # prints True if the character is in the string, # False otherwise. Do not use the in operator. s = input("Please enter any string: ") yourC = input("Please enter any character: ") result = False for c in s: if c == yourC: result = True print(result) print("X" in "bamba") # Q. 7 # Write a program that receives two strings, # s1 and s2, and prints all characters common # to the two strings. If there are no such # characters an empty string is printed. s1 = input("Please enter any string: ") s2 = input("Please enter any string: ") for c in s1: if c in s2: print(c) "Bamba" "BOmba" # without using in s1 = input("Please enter any string: ") s2 = input("Please enter any string: ") for c1 in s1: result = False for c2 in s2: if c1 == c2: result = True if result == True print(c1) # Q. 8 # Write a program that receives two strings, # s1 and s2. The program prints a string # including all characters found in s1 # but not in s2. If there are no such # characters an empty string is printed. s1 = input("Please enter any string: ") s2 = input("Please enter any string: ") found = False for c in s1: if c not in s2: print(c) found = True if not found: print("") 'BObma' 'Bamba' # Q. 9 # Write a program that receives a string # comprising only English words and spaces and # prints the number of upper-case letters # in the string. words = input("Please enter words serpated by spaces: ") counter = 0 for c in words: if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": counter = counter + 1 print("Number of upper-case letters: ", counter) I Have a dream # Q. 10 # Write a program that receives any list, # and prints the values in the list, one # after another, each in a separate line. lst = eval(input("Please enter a list: ")) for item in lst: print(item) lst = eval(input("Please enter a list: ")) print(type(lst)) print(lst) [3, 4, 5] # Q. 11 # Write a program that prints all numbers in range # 1-10 (including 10), each number in a different line. for num in range(1, 11): print(num) lst = list(range(2, 6)) print(lst) # Q. 12 # Write a program that calculates and prints the square # of all integers in range 1-10 (including 10). for num in range(1, 11): print(num ** 2) # Q. 13 # Write a program that receives an integer n # bigger than 2, and prints the remindars # resulting from dividing n by all numbers in # range 1 to n-1. n = int(input("Please enter an integer greater than 2: ")) for num in range(1, n): print(n % num) n == 5 1 2 3 4 0 1 2 1 # Q. 14 # Write a program that receives a list of integers, # and finds how many of them are bigger than 3. lst = eval(input("Please enter a list of integers: ")) counter = 0 for num in lst: if num > 3: counter = counter + 1 print("Number of integers greater than 3:", counter) # Q. 15 # Write a program that receives a list of integers, # and calculates their average. lst = eval(input("Please enter a list of integers: ")) counter = 0 sumNums = 0 for num in lst: sumNums = sumNums + num counter = counter + 1 print("Average is: ", sumNums / counter) # Q. 16 # Write a program that receives a list of integers, # and finds the biggest. # Do not use the function max(). lst = eval(input("Please enter a list of non-negative integers: ")) maxNum = 0 for num in lst: if num > maxNum: maxNum = num print(maxNum) print("Masimun number is: ", maxNum) # Q. 17 # Write a program that receives a list # of integers smaller than 100, and finds the # smallest even integer. if no even integer # exists in the list the program prints -1. lst = eval(input("Please enter a list of integers smaller than 100: ")) minNum = 99 for num in lst: if num % 2 == 0 and minNum > num: minNum = num print("Minimal number is:", minNum) 2 3 4 # REPETITION n = int(input("Please enter any positive integer: ")) for i in range(n): print("Kolargol") print("Procidis") # Q. 18 # Write a program that produces 20 random numbers # in range 0-100 (including 100), and prints them all. import random for i in range(20): print(random.randint(0, 100)) Q. 19 Write a program that receives two positive integers represeting the sides of a ractange, and then draws the rectangle using *s, E.g. if the integers are 5 and 4 the program prints: **** * * * * * * **** # version 1 side1 = int(input("Please enter a positive integer: ")) side2 = int(input("Please enter a positive integer: ")) print("*" * side2) for i in range(side1 - 2): print("*", end = "") for j in range(side2 -2): print(" ", end = "") print("*", end = "") print() print("*" * side2) # version 2 side1 = int(input("Please enter a positive integer: ")) side2 = int(input("Please enter a positive integer: ")) print("*" * side2) for i in range(side1 - 2): print("*" + " " * (side2 - 2) + "*") print("*" * side2) print("b" + "x") for i in range(side2): print('*', end = "") print(3, end = "") print(4)