ICS4U – Grade 11 Review Using Python

ICS4U Learning Goals

In this ICS4U lesson you will be reviewing the following programming concepts from grade 11:

  • Sequencing, Control, Repetition, Lists, Modularity

Prerequisite Concepts

Since ICS3U is a prerequisite for this course, there is an expectation that you already understand the concepts listed below and can solve problems using those concepts.

  • Displaying Output to the Screen
  • Getting information from the keyboard and storing it in variables
  • Using variables and math formulas to solve problems
  • Writing code to make decisions with the use of if / else statements
  • Writing code that repeats itself using loops
  • Storing data in arrays (lists)
  • Writing functions (methods) to solve simple tasks

Now, you might not have programmed in Python last year, but you should know how to solve coding problems and write programs using those concepts. If you understand those concepts well, then it should not be too much of a challenge to learn how to do it with Python. Below is a list of practice questions that you SHOULD be able to complete based on what you learned last year. The challenge will be that you need to code them in Python.

If your grade 11 teacher did not cover those concepts, you will have to put some extra effort into learning them quickly

I am going to provide you with links to my Grade 11 Material that I use in ICS3U for all of these concepts as a reference so you can learn the language. Remember that the resources below are meant to teach programming concepts as well as syntax. You are NOT required to complete ANY of the activities inside them. They are for REFERENCE ONLY. Use as much or as little as YOU need to in order to get familiar with Python

ICS4U Coding Questions

Try to code the following questions using an IDE of your choice.  You will save those files on your computer for future reference. 

Each question has:

  • A video of your teacher live coding and explaining the solution
  • The final code used in the video.

Try your best to solve the problems yourself without looking at the solutions.

Make sure you test your programs with multiple different inputs to ensure it is functioning properly.

Treat these questions like Math homework (Do as many as you feel you need to do to feel comfortable with the material)

Do some self reflection while completing these practice questions and assess if you have strong enough background skills to succeed in this course.  Are you willing to put in the extra time / effort to improve those skills to an adequate level?

ICS4U Practice Question

Name Your File:  “ICS4UquadraticFormula.py” 

Write a program that solves a quadratic equation in standard form using the quadratic formula. Check if the equation has zero, one, or two solutions first, using the discriminant, and then output any solutions (if there are any).

				
					#Input
print("Solving Ax^2 + Bx + C = 0")
A = float(input("Enter the value of A: "))
B = float(input("Enter the value of B: "))
C = float(input("Enter the value of C: "))

#Find the discrimenent of the quadratic formula
d = B**2 - 4*A*C

#Check for correct # of roots
if d > 0:
    ans1 = (-B + math.sqrt(d)) / (2*A)
    ans2 = (-B - math.sqrt(d)) / (2*A)
    print("The two solutions are x = ", ans1, ans2)
elif d == 0:
    ans = -B/(2*A)
    print("The only solution is x = ", ans)
else:
    print("No Real Solutions")

				
			

ICS4U Practice Question

Name Your File:  “ICS4Uattendance.py” 

A student is not allowed to write a University exam if their attendance is less than 75%. Write a program that accepts the number of classes held, the number of classes attended and print out the attendance % and if they are allowed to write the exam or not

				
					#Input
totalClasses = int(input("Enter total # of classes: "))
classesAttended = int(input("Enter total # of class attended: "))

#Find and output % attended
percent = classesAttended / totalClasses * 100
print("Student attended ", round(percent,2), "% of classes")

#Determine if exam can be written
if percent >= 75:
    print("They can write the exam")
else:
    print("They can't write the exam")
				
			

ICS4U Practice Question

Name Your File:  “ICS4Usequence.py” 

Let the user input a positive integer.

  • If the number is even divide it by 2.
  • If the number is odd multiply it by 3 and add 1.

Take the new number you generated and repeat the process. If you repeat this process enough times then eventually the number you generate will always equal to 1. Write a program outputs the sequence of numbers until it gets to 1

				
					#Input 
x = int(input("Enter a positive # larger than 1: "))

#Print first # of sequence, keep cursor on same line
print(x, end = " ")

#Loop until x = 1
while True:

    #Is x even?
    if x % 2 == 0:
        #Divide by 2 and keep as integer
        x = x // 2
    else:
        #Multiply by 3 and add 1
        x = 3*x + 1

    #Print new value and keep cursor on same line
    print(x, end=" ")

    #Stop when x = 1
    if x == 1:
        break

				
			

ICS4U Practice Question

Name Your File:  “ICS4UperfectNumbers.py” 

Create a function that determines if a number is a perfect number and then find the first 500 perfect numbers starting at 1. 

  • A number if perfect if all of its factors (excluding the number itself) add up to the number. 
  • 6 is perfect because it has factors 1,2,3,6 and 1 + 2 + 3 = 6

				
					#Returns True or False if x is perfect or not
def isPerfect(x):
    sum = 0
    #Find factors and add them up
    for i in range(1,x):

        #Found factor
        if x % i == 0:
            sum = sum + i

    #Check if perfect
    if sum == x:
        return True
    else:
        return False


#Main Program that solves the problem
def main():

    print("Perfect numbers less than 500")
    #Loop for all 500 #'s and print if perfect
    for i in range(1,501):
        if isPerfect(i):
            print(i)

#Call main program
main()

				
			

ICS4U Practice Question

Name Your File:  “ICS4UexponentsFunction.py” 

Create a function that does power functions a^b without using the built in exponents functions in python

				
					import random

#Calculates a^b
def power(a,b):
    p = 1
    for i in range(1,b+1):
        p = p * a

    return p

#Main program just to test
def main():

    #Print 10 Examples
    for i in range(0,10):
        a = random.randint(1,10)
        b = random.randint(0,8)
        ans = power(a,b)
        print(a,"^",b, "=", ans)

main()
				
			

ICS4U Practice Question

Name Your File:  “ICS4Upalindrome.py” 

Write a program that checks if a single word entered from the user is a palindrome.  Don’t use the built in “String Reverse” function python has.

				
					#get word
word = input("Enter the word: ")

#create the reverse word
letter = len(word)-1
reverse = ""
while letter >= 0:
    reverse = reverse + word[letter]
    letter = letter - 1

#Check for palindrome
if word == reverse:
    print("Palindrome")
else:
    print("Not Palindrome")
				
			

ICS4U Practice Question

Name Your File:  “ICS4UcountingList.py” 

Write a program that displays a random list of 75 numbers between 15 and 50 to the user.  Once that list is displayed, ask them for a target to search for.  Display how many of the target values are in the list

				
					import random

#Generate the random values and store in a list
values = []
for i in range(0,75):
    num = random.randint(15,50)
    values.append(num)
print(values)

#Get the target number
target = int(input("Count What? "))

#Search the list and increase the count
count = 0
for i in range (0,len(values)):
    if values[i] == target:
        count = count + 1
print("There were ",count,target,"'s")
				
			

ICS4U Practice Question

Name Your File:  “ICS4UalternatingList.py” 

Write a program that generates a random list of numbers between 0 and 100.  The size of the list is random between 4 and 20 and must an even number.  Output a new list in alternating reverse order

Ex.  If the list is [1,2,3,4,5,6] then output [1,6,2,5,3,4]

				
					import random

#Get a random size of list and make sure its an even # between 4 and 20
size = random.randint(4,20)
while size % 2 != 0:
    size = random.randint(4,20)

#Generate the random values in the list
myList = []
for i in range(0,size):
    myList.append(random.randint(0,100))

print("Original List: ", myList)

#Make the new list
newList = []
for i in range(0,size//2):

    #Start of list values
    newList.append(myList[i])

    #End of list values
    newList.append(myList[(size-1)-i])

print("Reverse Alternating List: ", newList)

				
			

ICS4U Practice Question

Name Your File:  “ICS4UkprNumbers.py” 

The KPR value of a number N is the smallest number that is divisible by all the integers between 1 and N. For example the KPR of 5 is 60 because 60 is the smallest number that is divisible by 1, 2, 3, 4, and 5. Display all the KPR values of the numbers from 1 to 20. Try to get your program to finish in less than 30 seconds

Here are some other KPR Values

  • KPR(13) = 360360
  • KPR(17) = 12252240
  • KPR(19) = 23792560

				
					#Finding KPR Numbers (aka Fractorials)
def KPR(n):
    f = n

    while True:

        #Count down loop from n so it breaks faster if not found
        for i in range(n, 0, -1):

            #Break if not a factor
            if f % i == 0:
                found = True
            else:
                found = False
                break
        if found:
            return f
        else:
            f = f + n # Only need to check multiples of n to make faster


# Main Program
for i in range(1, 21):
    print(i,": ", KPR(i))
				
			

Return to ICS4U Main Page