diff --git a/2020-computer-science-igcse-prerelease-pseudocode.txt b/2020-computer-science-igcse-prerelease-pseudocode.txt index bd4aacd..bef207c 100644 --- a/2020-computer-science-igcse-prerelease-pseudocode.txt +++ b/2020-computer-science-igcse-prerelease-pseudocode.txt @@ -1,4 +1,4 @@ -FUNCTION get_input( dict): +FUNCTION get_input(dict): tmp <- None WHILE (not tmp): tmp <- input().upper().strip() @@ -10,14 +10,14 @@ FUNCTION get_input( dict): RETURN tmp ENDFUNCTION -FUNCTION print_details( dict): +FUNCTION print_details(dict): FOR item in dict.keys(): OUTPUT "%s: $%.2f, %s" % (item, dict[item][0], dict[item][1]) ENDFOR ENDFUNCTION -FUNCTION print_chosen( dict, item): +FUNCTION print_chosen(dict, item): OUTPUT "%s: %s" % (item, dict[item][1]) RETURN dict[item][0] ENDFUNCTION @@ -45,19 +45,19 @@ run <- True devices <- 0 WHILE run: OUTPUT "Choose an option:" - print_details( devices_dict) - device <- get_input( devices_dict) + print_details(devices_dict) + device <- get_input(devices_dict) devices += 1 IF (device in phones_dict.keys()): OUTPUT "Choose an option:" - print_details( sims_dict) - sim_payg <- get_input( sims_dict) + print_details(sims_dict) + sim_payg <- get_input(sims_dict) ENDIF OUTPUT "Choose an option:" - print_details( case_dict) - case <- get_input( case_dict) + print_details(case_dict) + case <- get_input(case_dict) OUTPUT "Which charger(s) would you like? Enter the codes for those you want with a space between them, or enter nothing to choose neither" - print_details( charger_dict) + print_details(charger_dict) chargers <- input().upper().split(" ") FOR item in chargers: IF item not in charger_dict: @@ -65,17 +65,17 @@ WHILE run: ENDIF ENDFOR IF (devices > 1): - total += 0.9 * print_chosen( devices_dict, device) - saving += 0.1 * print_chosen( devices_dict, device) + total += 0.9 * print_chosen(devices_dict, device) + saving += 0.1 * print_chosen(devices_dict, device) ELSE: - total += print_chosen( devices_dict, device) + total += print_chosen(devices_dict, device) ENDIF IF (device in phones_dict): - total += print_chosen( sims_dict, sim_payg) + total += print_chosen(sims_dict, sim_payg) ENDIF - total += print_chosen( case_dict, case) + total += print_chosen(case_dict, case) FOR charger in chargers: - total += print_chosen( charger_dict, charger) + total += print_chosen(charger_dict, charger) ENDFOR OUTPUT "Subtotal: $%.2f" % total run <- True IF (input("Would you like another device (y/n)?").lower().strip()[0] = "y") ELSE False diff --git a/recursion-exercises.py b/recursion-exercises.py new file mode 100644 index 0000000..03160da --- /dev/null +++ b/recursion-exercises.py @@ -0,0 +1,40 @@ +# function to sum integers via recursion +def total(numbers): + # if recursion not needed due to too few items + if len(numbers) == 1: + return numbers[0] + return numbers[0] + total(numbers[1:]) + +# function to find if number is prime via recursion +def prime(number, divisor = None): + # create initial divisor, only if it doesn't exist + if not divisor: + # divide by two because all factors are duplicated - searching through all numbers smaller than the given number would search all factor pairs twice + # floor divide because if it's not an integer, it's not a valid factor - so can be ignored + # if rounded up, the number's compliment will appear again later, in the reverse of the factor pair + divisor = number // 2 + # number isn't a prime if less than two, or if it has a factor that's greater than one + if (number <= 1) or (divisor > 1 and number % divisor == 0): + return False + # all prime have been checked, and none are factors of the number + elif (divisor <= 1): + return True + else: + return prime(number, divisor - 1) + +# function for binary search via recursion +def search(item, list): + # get middle index (-1 accounts for zero-based indexing) + midpoint = (len(list) + 1) // 2 - 1 + # item found + if item == list[midpoint]: + return True + # all of list searched and item not found + elif (len(list) == 1): + return False + # item is larger than the middle item, so must be in the larger half of the list + elif item > list[midpoint]: + return search(item, list[midpoint + 1:]) + # item is smaller than the middle item, so must be in the smaller half of the list + else: + return search(item, list[:midpoint]) \ No newline at end of file