diff --git a/examples/Khan Sole Academy/credit.txt b/examples/Khan Sole Academy/credit.txt new file mode 100644 index 0000000..168fb10 --- /dev/null +++ b/examples/Khan Sole Academy/credit.txt @@ -0,0 +1 @@ +This solved example was written by Mbaoma Chioma Mary (Section Leader of Section 223) \ No newline at end of file diff --git a/examples/Khan Sole Academy/index.html b/examples/Khan Sole Academy/index.html new file mode 100644 index 0000000..c561e16 --- /dev/null +++ b/examples/Khan Sole Academy/index.html @@ -0,0 +1,66 @@ + + + + + Khan Sole Academy + + +Now that you’ve seen how programming can help us in a number of different areas, it’s +time for you to implement Khan-sole Academy—a program that helps other people learn! +In this problem, you’ll write a program in the file khansole_academy.py that randomly +generates simple addition problems for the user, reads in the answer from the user, and then +checks to see if they got it right or wrong, until the user appears to have mastered the +material. Note that “console” is another name for “terminal” :-).
+More specifically, your program should be able to generate simple addition problems that +involve adding two 2-digit integers (i.e., the numbers 10 through 99). The user should be +asked for an answer to each problem. Your program should determine if the answer was +correct or not, and give the user an appropriate message to let them know. Your program +should keep giving the user problems until the user has gotten 3 problems correct in a row. +
+(Note: the number of problems the user needs to get correctly in a row to complete the +program is just one example of a good place to specify a constant in your program).
+A sample run of the program is shown below (user input is in italics.) +
+ +Congratulations! You mastered addition.
+As a side note, one of the earliest programs Mehran wrote (with his friend Matthew) when +he was first learning how to program was very similar to Khan-Sole Academy and it was +called “M&M’s Math Puzzles”. It was written in a language named BASIC, on a computer +that had 4K of memory (that’s 4 Kilobytes) and used cassette tapes (the same kind used for +music in the 1970’s) to store information. Yeah, Mehran is old. + + + + \ No newline at end of file diff --git a/examples/Khan Sole Academy/soln.py b/examples/Khan Sole Academy/soln.py new file mode 100644 index 0000000..39c286e --- /dev/null +++ b/examples/Khan Sole Academy/soln.py @@ -0,0 +1,59 @@ +# a python program that randomly generates simple addition problems for the user, +# reads in the answer from the user, +# and then checks to see if they got it right or wrong, +# until the user appears to have mastered the material. + +#import the random library +import random + +#define your function +def main(): + + #we set the number of iterations to 0 as our starting value + iteration = 0 + + #we use a while loop so the keeps running as long as the user defined conditions are true + while True: + + #since we want the program to end after 3 correct consecutive answers, + #we state this condition in an 'if' statement + if iteration == 3: + print('Congratulations! You mastered addition.') + + #'break' keyword breaks out of the loop (ends the program) + break + + #if the 'if' block above is wrong, the 'else' code block would run + else: + num_one = random.randint(10, 99) + num_two = random.randint(10, 99) + result = num_one + num_two + + #we use the f formatting to set values of the variable names in curly brackets at + #the appropriate positions + print(f'What is {num_one} + {num_two}?') + + #we enable the user type in their answers and we convert it to a float using the + #'float' keyword + answer = float(input('Your answer: ')) + + #we define conditions to test the correctness of the user's input (answer) + if answer == result: + + #we increase the value of our iteration (we increase the starting value by 1) + iteration += 1 + + #the use of '/' is called escaping. It is used to inform the interpreter that, + #the character is part of a string + print(f'Correct! You\'ve gotten {iteration} correct in a row.') + + else: + print(f'Incorrect. The expected answer is {result}.') + + #the interation value is reduced by one, so the iteration starts counting all over from 1, + #when the if block above is true + iteration -= iteration + +#call your function +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/examples/Khan Sole Academy/title.txt b/examples/Khan Sole Academy/title.txt new file mode 100644 index 0000000..dcca7f1 --- /dev/null +++ b/examples/Khan Sole Academy/title.txt @@ -0,0 +1 @@ +The title of this solved example is Khan Sole Academy diff --git a/examples/index-game/credit.txt b/examples/index-game/credit.txt new file mode 100644 index 0000000..b869b75 --- /dev/null +++ b/examples/index-game/credit.txt @@ -0,0 +1 @@ +The example was solved by Mbaoma Chioma Mary (Section Leader for Section 223) \ No newline at end of file diff --git a/examples/index-game/index.html b/examples/index-game/index.html new file mode 100644 index 0000000..1161a61 --- /dev/null +++ b/examples/index-game/index.html @@ -0,0 +1,65 @@ + + + + + The Index Game + + + + We are going to take the first step into data science. You will learn about lists and files!
+

1. The Index Game

+As a warmup, read this code and play the game a few times. Use this mental model of the list:
+ + + + + + + + +
+ + + + + + + +
+
Value 'Julie' 'Mehran' 'Simba' 'Ayesha' 'Karel'
Index 0 1 2 3 4

+ +

def main():
+

# 1. Understand how to create a list and add values
+ # A list is an ordered collection of values
+ names = ['Julie', 'Mehran', 'Simba', 'Ayesha']
+ names.append( 'Karel' )
+ # 2. Understand how to loop over a list
+ # this prints the list to the screen one value at a time
+ for value in names:
+ print(value)
+ # 3. Understand how to look up the length of a list
+ # use randint to select a valid "index"
+ max_index = len(names) - 1
+ index = random.randint(0, max_index)
+ # 4. Understand how to get a value by its index
+ # get the item at the chosen index
+ correct_answer = names[index]
+ # This is just like in Khansole Academy...
+ # prompt the user for an answer, check if it is correct
+ prompt = 'who is in index...' + str(index)+ '? '
+ answer = input(prompt)
+ if answer == correct_answer:
+ print('Good job')
+ else:
+ print('Correct answer was' , correct_answer)


+ + + diff --git a/examples/index-game/soln.py b/examples/index-game/soln.py new file mode 100644 index 0000000..cb6b37c --- /dev/null +++ b/examples/index-game/soln.py @@ -0,0 +1,48 @@ +import random + +def main(): + #we set up a list of items + names = ['Julie', 'Mehran', 'Simba', 'Ayesha', 'Karel'] + + #.append is used to add an item to the end of a list + names.append('Omar') + + #we use a for loop to iterate through the list and print out individual members of the list + for name in names: + print(name) + + #len is used to get the length of a list + #i.e number of items in the list + length_of_list = len(names) + + #index of a list starts its count from 0 + max_index = length_of_list - 1 + + #f formats are used to print out strings + #te=he value in brace brakets can be any data type + print(f' the list has {length_of_list} items') + print(f'the list has a maximum index of {max_index}') + + #we set the variable name 'index' to a random value provided the value is a valid index + index = random.randint(0,max_index) + + #item_index prints the item at the random index above + item_index = names[index] + print(f'a random index is {index}') + print(f'an item at a randomly selected index is {item_index}') + + #who is in what index + print(names) + prompt = input(f'Who is at index {index}?: ') + correct_answer = names[index] + + #it is possible to assign two or more variable names to two or more values + #the .lower is used to set the values of prompt and correct_answer to lowercase to ensure that + #no matter the format the user enters the values, the interpreter converts it to lowercase + prompt , correct_answer = prompt.lower() , correct_answer.lower() + if prompt == correct_answer: + print('Correct!') + else: + print(f'Correct answer is, {correct_answer}') +if __name__ == '__main__': + main() diff --git a/examples/index-game/title.txt b/examples/index-game/title.txt new file mode 100644 index 0000000..2217ad8 --- /dev/null +++ b/examples/index-game/title.txt @@ -0,0 +1 @@ +The title of this solved example is Index Game (Python solution) diff --git a/examples/liftoff/credit.txt b/examples/liftoff/credit.txt new file mode 100644 index 0000000..168fb10 --- /dev/null +++ b/examples/liftoff/credit.txt @@ -0,0 +1 @@ +This solved example was written by Mbaoma Chioma Mary (Section Leader of Section 223) \ No newline at end of file diff --git a/examples/liftoff/index.html b/examples/liftoff/index.html new file mode 100644 index 0000000..8d028fd --- /dev/null +++ b/examples/liftoff/index.html @@ -0,0 +1,23 @@ + + + + + Liftoff + + +Write a program in the file liftoff.py that prints out the calls for a spaceship that is +about to launch. Countdown the numbers from 10 to 1 and then write “Liftoff!” Your +program should include a for loop using range. A sample run of the program is below.
+10
+9
+8
+7
+6
+5
+4
+3
+2
+1
+Liftoff! + + \ No newline at end of file diff --git a/examples/liftoff/soln.py b/examples/liftoff/soln.py new file mode 100644 index 0000000..c8ef954 --- /dev/null +++ b/examples/liftoff/soln.py @@ -0,0 +1,26 @@ +#a program that works like a countdown call +#the countdown runs from 10 -1 and then prints 'Liftoff' + +#define your function +def main(): + + #we use a for loop and the range funstion which has this format + #range(start, stop, step) + #start - starting value + #stop - stopping value + #step - format of printing the generated numbers + #we set the step value to a negative index so the countdown starts printing from the last + #number in the range (10) and stops at the first number (1) + for num in range(10,0,-1): + + #we print out the 'num' values in the range + print(num) + + #we print 'LIFTOFF!' outside of the loop so it appears at the end of the countdown and not + #inside the countdown sequence + print('Liftoff!') + +#call your function +if __name__ == '__main__': + main() + diff --git a/examples/liftoff/title.txt b/examples/liftoff/title.txt new file mode 100644 index 0000000..59da9d8 --- /dev/null +++ b/examples/liftoff/title.txt @@ -0,0 +1 @@ +The title of this solved example is Liftoff and it falls under the Sandcastles problem (Python solution) diff --git a/examples/random_numbers/credit.txt b/examples/random_numbers/credit.txt new file mode 100644 index 0000000..168fb10 --- /dev/null +++ b/examples/random_numbers/credit.txt @@ -0,0 +1 @@ +This solved example was written by Mbaoma Chioma Mary (Section Leader of Section 223) \ No newline at end of file diff --git a/examples/random_numbers/index.html b/examples/random_numbers/index.html new file mode 100644 index 0000000..33bdc02 --- /dev/null +++ b/examples/random_numbers/index.html @@ -0,0 +1,28 @@ + + + + + Random numbers + + +Write a program in the file random_numbers.py that prints 10 random integers (each +random integer should have a value between 0 and 100, inclusive). Your program +should use a constant named NUM_RANDOM, which determines the number of random +numbers to print (with a value of 10). It should also use constants named MIN_RANDOM +and MAX_RANDOM to determine the minimal and maximal values of the random numbers +generated (with respective values 0 and 100). To generate random numbers, you should +use the function random.randint() from Python’s random library.
+Example:
+35
+10
+45
+59
+45
+100
+8
+31
+48
+6
+It's alright if your code does not generate the exact numbers above. + + \ No newline at end of file diff --git a/examples/random_numbers/soln.py b/examples/random_numbers/soln.py new file mode 100644 index 0000000..db7d570 --- /dev/null +++ b/examples/random_numbers/soln.py @@ -0,0 +1,30 @@ +#we want to print 10 random numbers within the range of 0 and 100 (100 inclusive) +#we import the random library because we are printing a list of RANDOM numbers +import random + +#define your function +def main(): + #set the length of our proposed list of numbers + num_random = 10 + + #we set our starting value + min_random = 0 + + #we set our stopping value + max_random = 100 + + #we use a 'for' loop becuase we know the range/size of our list (i.e. 10) + #range is an inbuilt function in python that is used to specify the length of a list + #or number of iterations a funtion should undertake + for i in range(num_random): + + #the variable name 'random_num' contains the random integer to be generated by our script + #random.randint has this format random.randint(start, end) + random_num = random.randint(min_random, max_random) + + #we get a list of 10 numbers because we combined the random.randint function with a for loop + print(random_num) + +#call the function +if __name__ == '__main__': + main() diff --git a/examples/random_numbers/title.txt b/examples/random_numbers/title.txt new file mode 100644 index 0000000..2512207 --- /dev/null +++ b/examples/random_numbers/title.txt @@ -0,0 +1 @@ +The title of this solved example is Random numbers and it falls under the Sandcastles problem. diff --git a/examples/stroop/credit.txt b/examples/stroop/credit.txt new file mode 100644 index 0000000..08ac559 --- /dev/null +++ b/examples/stroop/credit.txt @@ -0,0 +1 @@ +This worked example was solved by Mbaoma Chioma Mary, section 223 leader \ No newline at end of file diff --git a/examples/stroop/index.html b/examples/stroop/index.html new file mode 100644 index 0000000..47e181f --- /dev/null +++ b/examples/stroop/index.html @@ -0,0 +1,28 @@ + + + + + Stroop + + +In psychology, the Stroop effect is a demonstration of cognitive interference where a delay in the reaction time of a task occurs due to a mismatch +in stimuli. The corresponding paper is one of the most cited in the history of Psychology. In this section we are going to implement a working version +of a Stroop test.
+Our stroop test has two phases:
+The first phase is the “control phase”. The user is shown color names written in the same font-color.
+Each time the user has to write the name of the color.
+Count how many times they type the name of the color correctly in 10 seconds
+Here are a few examples:
+

pink

+What color ink is the word written in? pink + +

red

+What color ink is the word written in? red + +

blue

+What color ink is the word written in? green
+Correct answer was blue. + + + + \ No newline at end of file diff --git a/examples/stroop/soln.py b/examples/stroop/soln.py new file mode 100644 index 0000000..cd165ac --- /dev/null +++ b/examples/stroop/soln.py @@ -0,0 +1,54 @@ +import random +import time +from colorama import init +from termcolor import colored +init(autoreset=True) + + +def main(): + #call the print_intro funcion + print_intro() + + #assign the random_color_name functin to a variable + color_name = random_color_name() + print(' ') + #call the print_in_color function and set the color_name to match the color of ink the color name would be printed in + #e.g if color_name is 'green', print the font 'green' in green ink + print_in_color(color_name , font_color= color_name) + response = input('What color ink is the word written in? ') + if response != color_name: + print('Correct answer was: ' + color_name) + return response == color_name + + +def print_intro(): + ''' + Function: print intro + Prints a simple welcome message + ''' + print('This is the Stroop test! Name the font-color used:') + #print_in_color('text', 'color') + print_in_color('red', 'red') + print_in_color('blue', 'blue') + print_in_color('pink', 'pink') + print_in_color('green', 'green') + +def random_color_name(): + ''' + Function: random color + Returns a string (either red, blue, pink or green) with equal likelihood + ''' + return random.choice(['red', 'blue', 'pink', 'green']) + +def print_in_color(text, font_color): + ''' + Function: print in color + Just like "print" but this time, you can specify the font-color + ''' + if font_color == 'pink': # magenta is a lot to type... + font_color = 'magenta' #another way of saying if the font color is pink, print the fonts in magenta ink + print(colored(text, font_color, attrs=['bold'])) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/examples/stroop/title.txt b/examples/stroop/title.txt new file mode 100644 index 0000000..ab931f9 --- /dev/null +++ b/examples/stroop/title.txt @@ -0,0 +1 @@ +The title of this worked example is stroop \ No newline at end of file diff --git a/examples/subtract_numbers/credit.txt b/examples/subtract_numbers/credit.txt new file mode 100644 index 0000000..b869b75 --- /dev/null +++ b/examples/subtract_numbers/credit.txt @@ -0,0 +1 @@ +The example was solved by Mbaoma Chioma Mary (Section Leader for Section 223) \ No newline at end of file diff --git a/examples/subtract_numbers/index.html b/examples/subtract_numbers/index.html new file mode 100644 index 0000000..485521f --- /dev/null +++ b/examples/subtract_numbers/index.html @@ -0,0 +1,21 @@ + + + + + Subtract numbers + + +Write a program in the file subtract_numbers.py that reads two real numbers from +the user and prints the first number minus the second number. You can assume the +user will always enter valid real numbers as input (negative values are fine). Yes, we +know this problem is really similar to a problem we did in class – that’s why this +problem is a sandcastle!
+A sample run of the program is shown below:
+This program subtracts one number from another.
+Enter first number: 5.5
+Enter second number: 2.1
+The result is 3.4
+ + + + diff --git a/examples/subtract_numbers/soln.py b/examples/subtract_numbers/soln.py new file mode 100644 index 0000000..1fe17c9 --- /dev/null +++ b/examples/subtract_numbers/soln.py @@ -0,0 +1,29 @@ +#subtract numbers +#a python script which accepts two numbers from the user, +#and subtracts the second from the first + +#define the function +def main(): + #print the title of the program + print('This program subtracts one number from another.') + + #accept the first input + #float keyword converts the input to a decimal number for uniformity + first_number = float(input('Enter first number: ')) + + #accept the second input + second_number = float(input('Enter second number: ')) + + #subtract the second input from the first input + result = first_number - second_number + + #i used f string formatting to display the final answer in a format that's more readable + #the variable name holding my answer to the subtraction is put in a curly bracket and placed at the + #position where i want it to be + print(f'The result is {result}') + +#call your function +if __name__ == '__main__': + main() + +#---------------------------------------------------------- diff --git a/examples/subtract_numbers/title.txt b/examples/subtract_numbers/title.txt new file mode 100644 index 0000000..eb2976b --- /dev/null +++ b/examples/subtract_numbers/title.txt @@ -0,0 +1 @@ +The title of this solved example is Subtract numbers and it falls under the Sandcastles problem.