Wrapup exercise - logic

Class objectives

  • Practice loops and conditionals in different cases
  • Advanced usage of variables
  • List comprihension basics

Character counter

Given a string variable of a sentence. Write a function with two parameters, return the number of target character in the string.

Input:

string = 'Practice makes perfect'

Write a function

def character_counter(string, char):
	counter = 0

	# process the string to count character

	return counter

Output:

print(character_counter(string, 'p')) # returns 2

Problem solving pattern

Solution 1:

  • Step 1: Define a function with two parameters character_counter(string, char)
  • Step 2: Define a variable initial value of 0 counter = 0
  • Step 3: Loop through the string to get each character
  • Step 4: Check if the character equels to the target character
  • Step 5: Increment counter by 1 if it is, continue if it’s not
  • Step 6: Return the final result at the end

Solution 2:

Solve it with list comprihension.

  • One line of code
  • You may consider list and len()

Students info

Given a list of lists, each list has two items, first item is age and second item is grade.

Data
Conditions
Category
[10, 4]
age < 15 and grade < 9 Junior
[11, 5]
age < 15 and grade < 9 Junior
[16, 10]
age > 15 and grade > 9 Senior
[17, 11]
age > 15 and grade > 9 Senior
[9, 3]
age < 15 and grade < 9 Junior

Write a function to categorize students info.

Input:

data = [[10, 4], [11, 5], [16, 10], [17, 11], [9, 3]]

Output:

['Junior', 'Junior', 'Senior', 'Senior', 'Junior']

Problem solving pattern

Solution 1:

  • Step 1: Define a function with a parameter
  • Step 2: Loop through the list data and check if satisfies conditions.
  • Step 3: Append the category in a new list and return

Solution 2:

Figure out a solution in list comprihension.

Multiplication table

Write a function to print multiplication table in following pattern.

Hint

Try nested for loops

Homework: Return factorial of 10

Factorial is the product of all positive integers less than or equal to N. In our case N = 10, therefore, the result is 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800

Write a function to return the factorial of 10.

How many solutions do you have?

You are encouraged to figure out the easiest solution.