├── .replit ├── Tic-Tac-Toe ├── tictactoe_part1.py ├── tictactoe_part2.py ├── tictactoe_part3.py ├── tictactoe_part4.py └── tictactoe.py ├── .gitattributes ├── Hangman ├── hangman_part1.py ├── hangman_part2.py ├── hangman_part3.py ├── hangman_part4.py ├── hangman_part5.py ├── hangman_part6.py ├── hangman_part7.py └── hangman_part8.py ├── Zookeeper ├── README.md ├── zookeeper_part1.py ├── zookeeper_part2.py ├── zookeeper_part3.py └── zookeeper.py ├── Simple Chatty Bot ├── Part1.py ├── Part2.py ├── README.md ├── Part3.py ├── Part4.py └── Part5.py ├── Coffee-Machine ├── coffee_machine_part1.py ├── coffee_machine_part2.py ├── coffee_machine_part3.py ├── coffee_machine_part4.py ├── coffee_machine_part5.py └── coffee_machine.py ├── README.md ├── scrape.py ├── LICENSE ├── checkpassword.py ├── data_structures.py ├── Text-Based-Browser └── browser_part1.py └── sample_python_code.py /.replit: -------------------------------------------------------------------------------- 1 | language = "python3" 2 | run = "echo hello word" -------------------------------------------------------------------------------- /Tic-Tac-Toe/tictactoe_part1.py: -------------------------------------------------------------------------------- 1 | print(''' 2 | X O X 3 | O X O 4 | X X O 5 | ''') -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Hangman/hangman_part1.py: -------------------------------------------------------------------------------- 1 | # Write your code here 2 | print("H A N G M A N") 3 | print("The game will be available soon.") -------------------------------------------------------------------------------- /Zookeeper/README.md: -------------------------------------------------------------------------------- 1 | # Zookeeper 2 | 3 | * Part 1 4 | * Print statements. 5 | * Part 2 6 | * Print image of camel to console. 7 | * Part 3 8 | * 9 | * Part 4 10 | * -------------------------------------------------------------------------------- /Zookeeper/zookeeper_part1.py: -------------------------------------------------------------------------------- 1 | print("I do love animals!") 2 | print("Start looking after animals...") 3 | print("Deer looks fine.") 4 | print("Bat looks happy.") 5 | print("Lion looks healthy.") -------------------------------------------------------------------------------- /Simple Chatty Bot/Part1.py: -------------------------------------------------------------------------------- 1 | print('Hello! My name is Aid.') 2 | print('I was created in 2020.') 3 | print('Please, remind me your name.') 4 | 5 | # reading a name 6 | 7 | print('What a great name you have, {yourName}!') -------------------------------------------------------------------------------- /Hangman/hangman_part2.py: -------------------------------------------------------------------------------- 1 | # Write your code here 2 | print("H A N G M A N") 3 | print("Guess the word: ") 4 | guess = input() 5 | if guess == "python": 6 | print("You survived!") 7 | else: 8 | print("You are hanged!") 9 | -------------------------------------------------------------------------------- /Simple Chatty Bot/Part2.py: -------------------------------------------------------------------------------- 1 | print('Hello! My name is Aid.') 2 | print('I was created in 2020.') 3 | print('Please, remind me your name.') 4 | 5 | # reading a name 6 | name = input() 7 | print('What a great name you have, ' + name + '!') -------------------------------------------------------------------------------- /Coffee-Machine/coffee_machine_part1.py: -------------------------------------------------------------------------------- 1 | print("Starting to make a coffee") 2 | print("Grinding coffee beans") 3 | print("Boiling water") 4 | print("Mixing boiled water with crushed coffee beans") 5 | print("Pouring coffee into the cup") 6 | print("Pouring some milk into the cup") 7 | print("Coffee is ready!") -------------------------------------------------------------------------------- /Hangman/hangman_part3.py: -------------------------------------------------------------------------------- 1 | # Write your code here 2 | import random 3 | 4 | print("H A N G M A N") 5 | print("Guess the word: ") 6 | word_list = ['python', 'java', 'kotlin', 'javascript'] 7 | word = random.choice(word_list) 8 | guess = input() 9 | if guess == word: 10 | print("You survived!") 11 | else: 12 | print("You are hanged!") 13 | -------------------------------------------------------------------------------- /Coffee-Machine/coffee_machine_part2.py: -------------------------------------------------------------------------------- 1 | drinks = int(input("Write how many cups of coffee you will need:")) 2 | water = 200 * drinks 3 | milk = 50 * drinks 4 | beans = 15 * drinks 5 | print("For " + str(drinks) + " cups of coffee you will need:") 6 | print(str(water) + " ml of water") 7 | print(str(milk) + " ml of milk") 8 | print(str(beans) + " g of coffee beans") 9 | -------------------------------------------------------------------------------- /Tic-Tac-Toe/tictactoe_part2.py: -------------------------------------------------------------------------------- 1 | user_input = input() 2 | print("---------") 3 | print("| " + str(user_input[0]) + " " 4 | + str(user_input[1]) + " " 5 | + str(user_input[2]) + " |") 6 | print("| " + str(user_input[3]) + " " 7 | + str(user_input[4]) + " " 8 | + str(user_input[5]) + " |") 9 | print("| " + str(user_input[6]) + " " 10 | + str(user_input[7]) + " " 11 | + str(user_input[8]) + " |") 12 | print("---------") -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Run on Repl.it](https://repl.it/badge/github/seakun/Python-Projects)](https://repl.it/github/seakun/Python-Projects) 2 | # Python Projects 3 | 4 | sample Python Projects 5 | 6 | * Simple Chatty Bot 7 | * Learned Python basic syntax and writing a simple program using variables, conditions, loops, and functions. 8 | 9 | * Zookeeper 10 | * Learned to use lists and access them with indexes. 11 | 12 | * Hangman 13 | * Learned about Random module. 14 | -------------------------------------------------------------------------------- /Hangman/hangman_part4.py: -------------------------------------------------------------------------------- 1 | # Write your code here 2 | import random 3 | 4 | print("H A N G M A N") 5 | 6 | word_list = ['python', 'java', 'kotlin', 'javascript'] 7 | word = random.choice(word_list) 8 | guess = input() 9 | 10 | word_formatted = guess[0:3] 11 | 12 | for i in range(0, len(guess) - 3): 13 | word_formatted += "-" 14 | 15 | print(f"Guess the word: {word_formatted}") 16 | if guess == word: 17 | print("You survived!") 18 | else: 19 | print("You are hanged!") 20 | -------------------------------------------------------------------------------- /Simple Chatty Bot/README.md: -------------------------------------------------------------------------------- 1 | # Simple Chatty Bot 2 | 3 | * Part 1 4 | * Write a bot who displays a greeting, its name and the date of its creation. 5 | * Part 2 6 | * Introduce input to the bot so that it can greet by name. 7 | * Part 3 8 | * Introduce input to the bot. It will greet by name and then try to guess the user's age using arithmetic operations. 9 | * Part 4 10 | * Program the bot to count from 0 to any positive number users enter. 11 | * Part 5 12 | * Give user you a test and check the answers. -------------------------------------------------------------------------------- /Simple Chatty Bot/Part3.py: -------------------------------------------------------------------------------- 1 | print('Hello! My name is Aid.') 2 | print('I was created in 2020.') 3 | print('Please, remind me your name.') 4 | 5 | name = input() 6 | 7 | print('What a great name you have, ' + name + '!') 8 | print('Let me guess your age.') 9 | print('Enter remainders of dividing your age by 3, 5 and 7.') 10 | 11 | # reading all remainders 12 | remainder3 = int(input()) 13 | remainder5 = int(input()) 14 | remainder7 = int(input()) 15 | 16 | age = (remainder3 * 70 + remainder5 * 21 + remainder7 * 15) % 105 17 | 18 | print("Your age is " + str(age) + "; that's a good time to start programming!") -------------------------------------------------------------------------------- /Simple Chatty Bot/Part4.py: -------------------------------------------------------------------------------- 1 | print('Hello! My name is Aid.') 2 | print('I was created in 2020.') 3 | print('Please, remind me your name.') 4 | 5 | name = input() 6 | 7 | print('What a great name you have, ' + name + '!') 8 | print('Let me guess your age.') 9 | print('Enter remainders of dividing your age by 3, 5 and 7.') 10 | 11 | rem3 = int(input()) 12 | rem5 = int(input()) 13 | rem7 = int(input()) 14 | 15 | age = (rem3 * 70 + rem5 * 21 + rem7 * 15) % 105 16 | 17 | print("Your age is " + str(age) + "; that's a good time to start programming!") 18 | print('Now I will prove to you that I can count to any number you want.') 19 | 20 | # read a number and count to it here 21 | num = int(input()) 22 | counter = 0 23 | while counter <= num: 24 | print(str(counter) + " !") 25 | counter += 1 26 | 27 | print('Completed, have a nice day!') 28 | -------------------------------------------------------------------------------- /Zookeeper/zookeeper_part2.py: -------------------------------------------------------------------------------- 1 | camel = """ 2 | Switching on camera from habitat with camels... 3 | ___.-''''-. 4 | /___ @ | 5 | ',,,,. | _.'''''''._ 6 | ' | / \\ 7 | | \ _.-' \\ 8 | | '.-' '-. 9 | | ', 10 | | '', 11 | ',,-, ':; 12 | ',,| ;,, ,' ;; 13 | ! ; !'',,,',',,,,'! ; ;: 14 | : ; ! ! ! ! ; ; :; 15 | ; ; ! ! ! ! ; ; ;, 16 | ; ; ! ! ! ! ; ; 17 | ; ; ! ! ! ! ; ; 18 | ;,, !,! !,! ;,; 19 | /_I L_I L_I /_I 20 | Yey, our little camel is sunbathing!""" 21 | 22 | # write your code here 23 | print(camel) -------------------------------------------------------------------------------- /Coffee-Machine/coffee_machine_part3.py: -------------------------------------------------------------------------------- 1 | water = int(input("Write how many ml of water the coffee machine has: ")) 2 | milk = int(input("Write how many ml of milk the coffee machine has: ")) 3 | beans = int(input("Write how many grams of coffee beans the coffee machine has: ")) 4 | drinks = int(input("Write how many cups of coffee you will need: ")) 5 | max_cups = min(water // 200, milk // 50, beans // 15) 6 | if drinks < max_cups: 7 | print("Yes, I can make that amount of coffee (and even " 8 | + str(max_cups - drinks) 9 | + " more than that)") 10 | elif water - (drinks * 200) >= 0 \ 11 | and milk - (drinks * 50) >= 0 \ 12 | and beans - (drinks * 15) >= 0: 13 | print("Yes, I can make that amount of coffee.") 14 | else: 15 | print("No, I can make only " 16 | + str(min(water // 200, milk // 50, beans // 15)) 17 | + " cups of coffee.") 18 | -------------------------------------------------------------------------------- /scrape.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from bs4 import BeautifulSoup 3 | import pprint 4 | 5 | res = requests.get('https://lobste.rs/') 6 | soup = BeautifulSoup(res.text, 'html.parser') 7 | lobster_links = soup.select('.u-url') 8 | lobster_subtext = soup.select('.story') 9 | 10 | 11 | def create_custom_lb_list(links, subtext): 12 | lb = [] 13 | for index, item in enumerate(links): 14 | title = item.getText() 15 | href = item.get('href', None) 16 | score = subtext[index].select('.score') 17 | if len(score): 18 | points = int(score[0].getText()) 19 | if points > 10: 20 | lb.append({'title': title, 'link': href, 'score': points}) 21 | return sort_stories_by_score(lb) 22 | 23 | 24 | def sort_stories_by_score(lb_list): 25 | return sorted(lb_list, key=lambda k: k['score'], reverse=True) 26 | 27 | 28 | pprint.pprint(create_custom_lb_list(lobster_links, lobster_subtext)) 29 | -------------------------------------------------------------------------------- /Hangman/hangman_part5.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | def hide_word(letters, lst = []): 5 | for letter in letters: 6 | if letter not in lst: 7 | letters = letters.replace(letter, "-") 8 | return letters 9 | 10 | 11 | def hangman(): 12 | print("H A N G M A N") 13 | print() 14 | words_list = ['python', 'java', 'kotlin', 'javascript'] 15 | word = random.choice(words_list) 16 | num_of_tries = 8 17 | 18 | word_start = hide_word(word) 19 | guessed_letters = [] 20 | 21 | for i in range(num_of_tries): 22 | print(word_start) 23 | guess = input("Input a letter: ") 24 | if guess in word and guess not in guessed_letters: 25 | guessed_letters.append(guess) 26 | word_start = hide_word(word, guessed_letters) 27 | print() 28 | else: 29 | print("No such letter in the word\n") 30 | print("Thanks for playing!\nWe'll see how well you did in the next stage") 31 | 32 | 33 | hangman() 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sean Kung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /checkpassword.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import hashlib 3 | import sys 4 | 5 | 6 | def request_api_data(query_char): 7 | url = 'https://api.pwnedpasswords.com/range/' + query_char 8 | res = requests.get(url) 9 | if res.status_code != 200: 10 | raise RuntimeError(f'Error fetching: {res.status_code}, check the API and try again') 11 | return res 12 | 13 | 14 | def get_password_leaks_count(hashes, hash_to_check): 15 | hashes = (line.split(':') for line in hashes.text.splitlines()) 16 | for h, count in hashes: 17 | if h == hash_to_check: 18 | return count 19 | return 0 20 | 21 | 22 | def pwned_api_check(password): 23 | sha1password = hashlib.sha1(password.encode('UTF-8')).hexdigest().upper() 24 | first5_char, tail = sha1password[:5], sha1password[5:] 25 | response = request_api_data(first5_char) 26 | return get_password_leaks_count(response, tail) 27 | 28 | 29 | def main(args): 30 | for password in args: 31 | count = pwned_api_check(password) 32 | if count: 33 | print(f'{password} was found {count} times... you should probably change your password') 34 | else: 35 | print(f'{password} was NOT found. Carry on!') 36 | return 'done!' 37 | 38 | if __name__ == '__main__': 39 | sys.exit(main(sys.argv[1:])) 40 | 41 | -------------------------------------------------------------------------------- /Hangman/hangman_part6.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | # Write your code here 4 | print("H A N G M A N") 5 | print("") 6 | 7 | # computer takes random word 8 | r = random.choice(['python', 'java', 'kotlin', 'javascript']) 9 | new_word = [] 10 | # word split up into elements 11 | split = list(r) 12 | for i in range(len(split)): 13 | new_word.append("-") 14 | print(str("".join(new_word))) 15 | 16 | # asking user for letter 17 | i = 8 18 | while i != 0: 19 | print("Input a letter:", end='') 20 | new_letter = input().strip() 21 | # case if letter not in word 22 | if new_letter not in split: 23 | print("No such letter in the word") 24 | i -= 1 25 | # checking if the user has already asked for this letter 26 | if new_letter in new_word: 27 | print("No improvements") 28 | i -= 1 29 | # end game if user loses all tries 30 | if "-" in new_word and i == 0: 31 | print("You are hanged!") 32 | break 33 | print("") 34 | # checking if letter is in the word 35 | for p, e in enumerate(split): 36 | if e == new_letter: 37 | new_word[p] = new_letter 38 | # checking if the letter has already been asked 39 | print(str("".join(new_word))) 40 | # end game if user got the word 41 | if new_word == split: 42 | print("You guessed the word!") 43 | print("You survived!") 44 | break 45 | 46 | -------------------------------------------------------------------------------- /data_structures.py: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # 2D Array - DS 4 | #!/bin/python3 5 | 6 | import math 7 | import os 8 | import random 9 | import re 10 | import sys 11 | 12 | # Complete the hourglassSum function below. 13 | def hourglassSum(arr): 14 | hourglass_totals = [] 15 | for x in range(0, 4): 16 | for y in range(0, 4): 17 | hourglass_totals.append(arr[x][y] + arr[x][y + 1] + arr[x][y + 2] 18 | + arr[x + 1][y + 1] 19 | + arr[x + 2][y] + arr[x + 2][y + 1] + arr[x + 2][y + 2]) 20 | print(hourglass_totals) 21 | return max(hourglass_totals) 22 | if __name__ == '__main__': 23 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 24 | 25 | arr = [] 26 | 27 | for _ in range(6): 28 | arr.append(list(map(int, input().rstrip().split()))) 29 | 30 | result = hourglassSum(arr) 31 | 32 | fptr.write(str(result) + '\n') 33 | 34 | fptr.close() 35 | 36 | # Arrays - DS 37 | #!/bin/python3 38 | 39 | import math 40 | import os 41 | import random 42 | import re 43 | import sys 44 | 45 | # Complete the reverseArray function below. 46 | def reverseArray(a): 47 | reverse_array = [] 48 | for i in range(len(a) - 1, -1, -1): 49 | reverse_array.append(arr[i]) 50 | return reverse_array 51 | if __name__ == '__main__': 52 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 53 | 54 | arr_count = int(input()) 55 | 56 | arr = list(map(int, input().rstrip().split())) 57 | 58 | res = reverseArray(arr) 59 | 60 | fptr.write(' '.join(map(str, res))) 61 | fptr.write('\n') 62 | 63 | fptr.close() 64 | -------------------------------------------------------------------------------- /Text-Based-Browser/browser_part1.py: -------------------------------------------------------------------------------- 1 | 2 | nytimes_com = ''' 3 | This New Liquid Is Magnetic, and Mesmerizing 4 | 5 | Scientists have created “soft” magnets that can flow 6 | and change shape, and that could be a boon to medicine 7 | and robotics. (Source: New York Times) 8 | 9 | 10 | Most Wikipedia Profiles Are of Men. This Scientist Is Changing That. 11 | 12 | Jessica Wade has added nearly 700 Wikipedia biographies for 13 | important female and minority scientists in less than two 14 | years. 15 | 16 | ''' 17 | 18 | bloomberg_com = ''' 19 | The Space Race: From Apollo 11 to Elon Musk 20 | 21 | It's 50 years since the world was gripped by historic images 22 | of Apollo 11, and Neil Armstrong -- the first man to walk 23 | on the moon. It was the height of the Cold War, and the charts 24 | were filled with David Bowie's Space Oddity, and Creedence's 25 | Bad Moon Rising. The world is a very different place than 26 | it was 5 decades ago. But how has the space race changed since 27 | the summer of '69? (Source: Bloomberg) 28 | 29 | 30 | Twitter CEO Jack Dorsey Gives Talk at Apple Headquarters 31 | 32 | Twitter and Square Chief Executive Officer Jack Dorsey 33 | addressed Apple Inc. employees at the iPhone maker’s headquarters 34 | Tuesday, a signal of the strong ties between the Silicon Valley giants. 35 | ''' 36 | 37 | # write your code here 38 | website = input() 39 | 40 | while True: 41 | if website == "bloomberg.com": 42 | print(bloomberg_com) 43 | elif website == "nytimes.com": 44 | print(nytimes_com) 45 | elif website == "exit": 46 | break 47 | 48 | website = input() 49 | -------------------------------------------------------------------------------- /Coffee-Machine/coffee_machine_part4.py: -------------------------------------------------------------------------------- 1 | water = 1200 2 | milk = 540 3 | beans = 120 4 | cups = 9 5 | money = 550 6 | 7 | 8 | def print_state(): 9 | print("The coffee machine has: ") 10 | print(str(water) + " of water") 11 | print(str(milk) + " of milk") 12 | print(str(beans) + " of coffee beans") 13 | print(str(cups) + " of disposable cups") 14 | print(str(money) + " of money") 15 | 16 | 17 | print_state() 18 | 19 | action = input("Write action (buy, fill, take): ") 20 | 21 | if action == "fill": 22 | add_water = int(input("Write how many ml of water do you want to add:")) 23 | water += add_water 24 | add_milk = int(input("Write how many ml of milk do you want to add:")) 25 | milk += add_milk 26 | add_beans = int(input("Write how many grams of coffee beans do you want to add:")) 27 | beans += add_beans 28 | add_cups = int(input("Write how many disposable cups of coffee do you want to add:")) 29 | cups += add_cups 30 | print_state() 31 | elif action == "take": 32 | print("I gave you " + str(money)) 33 | money -= money 34 | print_state() 35 | else: 36 | options = int(input("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:")) 37 | if options == 1: 38 | water -= 250 39 | beans -= 16 40 | cups -= 1 41 | money += 4 42 | elif options == 2: 43 | water -= 350 44 | milk -= 75 45 | beans -= 20 46 | cups -= 1 47 | money += 7 48 | else: 49 | water -= 200 50 | milk -= 100 51 | beans -= 12 52 | cups -= 1 53 | money += 6 54 | print_state() 55 | -------------------------------------------------------------------------------- /Hangman/hangman_part7.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | words_list = ['python', 'java', 'kotlin', 'javascript'] 4 | word = random.choice(words_list) 5 | 6 | def hide_word(word_to_hide, list_of_letters): 7 | for letter in word_to_hide: 8 | if letter not in list_of_letters: 9 | word_to_hide = word_to_hide.replace(letter, "-") 10 | return word_to_hide 11 | 12 | print("H A N G M A N\n") 13 | num_of_tries = 8 14 | guessed_letters = [] 15 | all_input_letters = [] 16 | word_start = hide_word(word, guessed_letters) 17 | 18 | while num_of_tries > 0: 19 | print(word_start) 20 | guess = input("Input a letter: ") 21 | 22 | if len(guess) != 1: 23 | print('You should print a single letter\n') 24 | continue 25 | 26 | elif 97 > ord(guess) or ord(guess) > 122: 27 | print('It is not an ASCII lowercase letter\n') 28 | continue 29 | 30 | elif guess in all_input_letters: 31 | print('You already typed this letter\n') 32 | continue 33 | 34 | all_input_letters.append(guess) 35 | 36 | if guess in word and guess not in guessed_letters: 37 | guessed_letters.append(guess) 38 | word_start = hide_word(word, guessed_letters) 39 | print() 40 | if "-" not in word_start: 41 | print(word_start) 42 | print("You guessed the word!\n" 43 | "You survived!") 44 | break 45 | 46 | else: 47 | num_of_tries -= 1 48 | if num_of_tries == 0: 49 | print("No such letter in the word\n" 50 | "You are hanged!") 51 | else: 52 | print("No such letter in the word\n") 53 | -------------------------------------------------------------------------------- /Simple Chatty Bot/Part5.py: -------------------------------------------------------------------------------- 1 | def greet(bot_name, birth_year): 2 | print('Hello! My name is ' + bot_name + '.') 3 | print('I was created in ' + birth_year + '.') 4 | 5 | 6 | def remind_name(): 7 | print('Please, remind me your name.') 8 | name = input() 9 | print('What a great name you have, ' + name + '!') 10 | 11 | 12 | def guess_age(): 13 | print('Let me guess your age.') 14 | print('Enter remainders of dividing your age by 3, 5 and 7.') 15 | 16 | rem3 = int(input()) 17 | rem5 = int(input()) 18 | rem7 = int(input()) 19 | age = (rem3 * 70 + rem5 * 21 + rem7 * 15) % 105 20 | 21 | print("Your age is " + str(age) + "; that's a good time to start programming!") 22 | 23 | 24 | def count(): 25 | print('Now I will prove to you that I can count to any number you want.') 26 | 27 | num = int(input()) 28 | curr = 0 29 | while curr <= num: 30 | print(curr, '!') 31 | curr = curr + 1 32 | 33 | 34 | def test(): 35 | print("Let's test your programming knowledge.") 36 | print("Why do we use methods?") 37 | print("1. To repeat a statement multiple times.") 38 | print("2. To decompose a program into several small subroutines.") 39 | print("3. To determine the execution time of a program.") 40 | print("4. To interrupt the execution of a program.") 41 | user_ans = int(input()) 42 | while user_ans != 2: 43 | print("Please, try again.") 44 | user_ans = int(input()) 45 | print('Completed, have a nice day!') 46 | 47 | 48 | def end(): 49 | print('Congratulations, have a nice day!') 50 | 51 | 52 | greet('Aid', '2020') # change it as you need 53 | remind_name() 54 | guess_age() 55 | count() 56 | test() 57 | end() 58 | -------------------------------------------------------------------------------- /Hangman/hangman_part8.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | words_list = ['python', 'java', 'kotlin', 'javascript'] 4 | word = random.choice(words_list) 5 | 6 | def hide_word(word_to_hide, list_of_letters): 7 | for letter in word_to_hide: 8 | if letter not in list_of_letters: 9 | word_to_hide = word_to_hide.replace(letter, "-") 10 | return word_to_hide 11 | 12 | def game(): 13 | num_of_tries = 8 14 | guessed_letters = [] 15 | all_input_letters = [] 16 | word_start = hide_word(word, guessed_letters) 17 | 18 | while num_of_tries > 0: 19 | print(word_start) 20 | guess = input("Input a letter: ") 21 | 22 | if len(guess) != 1: 23 | print('You should print a single letter\n') 24 | continue 25 | 26 | elif 97 > ord(guess) or ord(guess) > 122: 27 | print('It is not an ASCII lowercase letter\n') 28 | continue 29 | 30 | elif guess in all_input_letters: 31 | print('You already typed this letter\n') 32 | continue 33 | 34 | all_input_letters.append(guess) 35 | 36 | if guess in word and guess not in guessed_letters: 37 | guessed_letters.append(guess) 38 | word_start = hide_word(word, guessed_letters) 39 | print() 40 | if "-" not in word_start: 41 | print(word_start) 42 | print("You guessed the word!\n" 43 | "You survived!") 44 | break 45 | 46 | else: 47 | num_of_tries -= 1 48 | if num_of_tries == 0: 49 | print("No such letter in the word\n" 50 | "You are hanged!") 51 | else: 52 | print("No such letter in the word\n") 53 | 54 | def menu(): 55 | print("H A N G M A N\n") 56 | user_input = input('Type "play" to play the game, "exit" to quit: ') 57 | if user_input == "play": 58 | game() 59 | elif user_input == "exit": 60 | return; 61 | else: 62 | menu() 63 | -------------------------------------------------------------------------------- /Tic-Tac-Toe/tictactoe_part3.py: -------------------------------------------------------------------------------- 1 | # write your code here 2 | field = input("Enter cells: ") 3 | player_1 = "X" 4 | player_2 = "O" 5 | 6 | game_board = [[field[0], field[1], field[2]], 7 | [field[3], field[4], field[5]], 8 | [field[6], field[7], field[8]]] 9 | 10 | print("---------") 11 | print("| " + " ".join(game_board[0]) + " |") 12 | print("| " + " ".join(game_board[1]) + " |") 13 | print("| " + " ".join(game_board[2]) + " |") 14 | print("---------") 15 | 16 | 17 | def win_status(player): 18 | # check rows 19 | for row in game_board: 20 | if row.count(player) == 3: 21 | return True 22 | # check columns 23 | column = [[], [], []] 24 | for x in range(0, 3): 25 | for row in game_board: 26 | column[x].append(row[x]) 27 | if column[x].count(player) == 3: 28 | return True 29 | # check diagonals 30 | diagonal_1 = [game_board[0][0], game_board[1][1], game_board[2][2]] 31 | diagonal_2 = [game_board[0][2], game_board[1][1], game_board[2][0]] 32 | if diagonal_1.count(player) == 3 or diagonal_2.count(player) == 3: 33 | return True 34 | 35 | 36 | def finished(board): 37 | for i in range(len(board)): 38 | if "_" in board[i]: 39 | return True 40 | 41 | 42 | def too_many(x, o): 43 | num_x = [square for row in game_board for square in row if square == x] 44 | num_y = [square for row in game_board for square in row if square == o] 45 | if abs(len(num_x) - len(num_y)) >= 2: 46 | return True 47 | 48 | 49 | def print_outcome(x, o): 50 | win_status(x) 51 | win_status(o) 52 | if win_status(x) and win_status(o): 53 | print("Impossible") 54 | elif too_many(x, o) is True: 55 | print("Impossible") 56 | elif win_status(x): 57 | print(f"{x} wins") 58 | elif win_status(o): 59 | print(f"{o} wins") 60 | elif win_status(x) is not True and win_status(o) is not True: 61 | if finished(game_board) is True: 62 | print("Game not finished") 63 | else: 64 | print("Draw") 65 | 66 | print_outcome(player_1, player_2) 67 | -------------------------------------------------------------------------------- /Coffee-Machine/coffee_machine_part5.py: -------------------------------------------------------------------------------- 1 | water = 400 2 | milk = 540 3 | beans = 120 4 | cups = 9 5 | money = 550 6 | 7 | action = input("Write action (buy, fill, take, remaining, exit): ") 8 | 9 | while action != "exit": 10 | if action == "remaining": 11 | print("The coffee machine has: ") 12 | print(str(water) + " of water") 13 | print(str(milk) + " of milk") 14 | print(str(beans) + " of coffee beans") 15 | print(str(cups) + " of disposable cups") 16 | print(str(money) + " of money") 17 | elif action == "fill": 18 | add_water = int(input("Write how many ml of water do you want to add:")) 19 | water += add_water 20 | add_milk = int(input("Write how many ml of milk do you want to add:")) 21 | milk += add_milk 22 | add_beans = int(input("Write how many grams of coffee beans do you want to add:")) 23 | beans += add_beans 24 | add_cups = int(input("Write how many disposable cups of coffee do you want to add:")) 25 | cups += add_cups 26 | elif action == "take": 27 | print("I gave you " + str(money)) 28 | money -= money 29 | else: 30 | while True: 31 | options = input("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:") 32 | if options == "1": 33 | if min(water - 250, beans - 16, cups - 1) < 0: 34 | if water - 250 < 0: 35 | print("Sorry, not enough " + str(water) + "!") 36 | elif beans - 16 < 0: 37 | print("Sorry, not enough " + str(beans) + "!") 38 | else: 39 | print("Sorry, not enough " + str(cups) + "!") 40 | else: 41 | print("I have enough resources, making you a coffee!") 42 | water -= 250 43 | beans -= 16 44 | cups -= 1 45 | money += 4 46 | elif options == "2": 47 | if min(water - 350, milk - 75, beans - 16, cups - 1) < 0: 48 | if water - 350 < 0: 49 | print("Sorry, not enough " + str(water) + "!") 50 | elif milk - 75 < 0: 51 | print("Sorry, not enough " + str(milk) + "!") 52 | elif beans - 20 < 0: 53 | print("Sorry, not enough " + str(beans) + "!") 54 | else: 55 | print("Sorry, not enough " + str(cups) + "!") 56 | else: 57 | print("I have enough resources, making you a coffee!") 58 | water -= 350 59 | milk -= 75 60 | beans -= 20 61 | cups -= 1 62 | money += 7 63 | elif options == "3": 64 | if min(water - 200, milk - 100, beans - 12, cups - 1) < 0: 65 | if water - 200 < 0: 66 | print("Sorry, not enough " + str(water) + "!") 67 | elif milk - 100 < 0: 68 | print("Sorry, not enough " + str(milk) + "!") 69 | elif beans - 12 < 0: 70 | print("Sorry, not enough " + str(beans) + "!") 71 | else: 72 | print("Sorry, not enough " + str(cups) + "!") 73 | else: 74 | print("I have enough resources, making you a coffee!") 75 | water -= 200 76 | milk -= 100 77 | beans -= 12 78 | cups -= 1 79 | money += 6 80 | else: 81 | break 82 | 83 | action = input("Write action (buy, fill, take, remaining, exit): ") 84 | -------------------------------------------------------------------------------- /Coffee-Machine/coffee_machine.py: -------------------------------------------------------------------------------- 1 | def show_state_machine(): 2 | print("The coffee machine has:") 3 | print(water, " of water") 4 | print(milk, " of milk") 5 | print(coffee_beans, " of coffee beans") 6 | print(disposable_cups, " of disposable cups") 7 | print("$", money, " of money") 8 | 9 | 10 | def verify_resources(water_desc, milk_desc, coffee_beans_desc, disposable_cups_desc, money_desc): 11 | global water, milk, coffee_beans, disposable_cups, money 12 | if water < water_desc: 13 | return "Sorry,not enough water!" 14 | elif milk < milk_desc: 15 | return "Sorry,not enough milk!" 16 | elif coffee_beans < coffee_beans_desc: 17 | return "Sorry,not enough coffee beans!" 18 | elif disposable_cups < disposable_cups_desc: 19 | return "Sorry,not enough disposable cups!" 20 | elif money < money_desc: 21 | return "Sorry,not enough money!" 22 | else: 23 | return "" 24 | 25 | 26 | def manage_resources(water_desc, milk_desc, coffee_beans_desc, disposable_cups_desc, money_desc): 27 | global water, milk, coffee_beans, disposable_cups, money 28 | water += water_desc 29 | milk += milk_desc 30 | coffee_beans += coffee_beans_desc 31 | disposable_cups += disposable_cups_desc 32 | money += money_desc 33 | 34 | 35 | def buy(): 36 | while True: 37 | choice = input("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:") 38 | if choice == "1": 39 | if verify_resources(250, 0, 16, 1, -4) != "": 40 | print(verify_resources(250, 0, 16, 1, -4)) 41 | break 42 | else: 43 | print("I have enough resources, making you a coffee!") 44 | manage_resources(-250, 0, -16, -1, 4) 45 | break 46 | elif choice == "2": 47 | if verify_resources(250, 0, 16, 1, -4) != "": 48 | print(verify_resources(250, 0, 16, 1, -4)) 49 | break 50 | else: 51 | print("I have enough resources, making you a coffee!") 52 | manage_resources(-350, -75, -20, -1, 7) 53 | break 54 | elif choice == "3": 55 | if verify_resources(250, 0, 16, 1, -4) != "": 56 | print(verify_resources(250, 0, 16, 1, -4)) 57 | break 58 | else: 59 | print("I have enough resources, making you a coffee!") 60 | manage_resources(-200, -100, -12, -1, 6) 61 | break 62 | elif choice == "back": 63 | break 64 | 65 | 66 | def fill(): 67 | water_added = int(input("Write how many ml of water do you want to add: ")) 68 | milk_added = int(input("Write how many ml of milk do you want to add: ")) 69 | coffee_beans_added = int(input("Write how many grams of coffee beans do you want to add: ")) 70 | disposable_cups_added = int(input("Write how many disposable cups of coffee do you want to add: ")) 71 | manage_resources(water_added, milk_added, coffee_beans_added, disposable_cups_added, 0) 72 | 73 | 74 | def take(): 75 | print("I gave you $", money) 76 | manage_resources(0, 0, 0, 0, -money) 77 | 78 | 79 | def select(): 80 | while True: 81 | choice = str(input("Write action (buy, fill, take, remaining, exit): ")) 82 | if choice == "buy": 83 | buy() 84 | elif choice == "fill": 85 | fill() 86 | elif choice == "take": 87 | take() 88 | elif choice == "remaining": 89 | show_state_machine() 90 | elif choice == "exit": 91 | break 92 | 93 | 94 | water = 400 95 | milk = 540 96 | coffee_beans = 120 97 | disposable_cups = 9 98 | money = 550 99 | select() 100 | -------------------------------------------------------------------------------- /Tic-Tac-Toe/tictactoe_part4.py: -------------------------------------------------------------------------------- 1 | # write your code here 2 | user_field = input("Enter cells: ") 3 | game_board = [] 4 | player_1 = "X" 5 | player_2 = "O" 6 | 7 | 8 | def print_field(field): 9 | global game_board 10 | coordinates = list() 11 | game_board = [[field[0], field[1], field[2]], 12 | [field[3], field[4], field[5]], 13 | [field[6], field[7], field[8]]] 14 | 15 | def printer(board): 16 | print("---------") 17 | print("| " + " ".join(board[0]) + " |") 18 | print("| " + " ".join(board[1]) + " |") 19 | print("| " + " ".join(board[2]) + " |") 20 | print("---------") 21 | 22 | printer(game_board) 23 | 24 | field = input("Enter the coordinates: ").split() 25 | 26 | def validation(user_input): 27 | # validate input type 28 | for item in user_input: 29 | if item.isnumeric(): 30 | if 1 <= int(item) <= 3: 31 | # - 1 to match matrix coordinate (0-2 vs 1-3) 32 | coordinates.append(int(item) - 1) 33 | else: 34 | print("Coordinates should be from 1 to 3!") 35 | coordinates.clear() 36 | return False 37 | else: 38 | print("You should enter numbers!") 39 | coordinates.clear() 40 | return False 41 | del coordinates[2:] 42 | # adjust coordinates for matrix output 43 | coordinates[1] = abs(coordinates[1] - 2) 44 | coordinates.reverse() 45 | print("CALCULATED coordinates ", coordinates) 46 | if game_board[coordinates[0]][coordinates[1]] == "_": 47 | # trim extra input items if coordinate list is too long 48 | return coordinates, True 49 | else: 50 | print("This cell is occupied! Choose another one!") 51 | coordinates.clear() 52 | return False 53 | 54 | while validation(field) is False: 55 | field = input("Enter the NEW coordinates: ").split() 56 | 57 | # update game board with new move 58 | game_board[coordinates[0]][coordinates[1]] = player_1 59 | printer(game_board) 60 | return field 61 | 62 | 63 | def print_game_status(): 64 | def win_status(player): 65 | # check rows 66 | for row in game_board: 67 | if row.count(player) == 3: 68 | return True 69 | # check columns 70 | column = [[], [], []] 71 | for x in range(0, 3): 72 | for row in game_board: 73 | column[x].append(row[x]) 74 | if column[x].count(player) == 3: 75 | return True 76 | # check diagonals 77 | diagonal_1 = [game_board[0][0], game_board[1][1], game_board[2][2]] 78 | diagonal_2 = [game_board[0][2], game_board[1][1], game_board[2][0]] 79 | if diagonal_1.count(player) == 3 or diagonal_2.count(player) == 3: 80 | return True 81 | 82 | def finished(board): 83 | for i in range(len(board)): 84 | if "_" in board[i]: 85 | return True 86 | 87 | def too_many(x, o): 88 | num_x = [square for row in game_board for square in row if square == x] 89 | num_y = [square for row in game_board for square in row if square == o] 90 | if abs(len(num_x) - len(num_y)) >= 2: 91 | return True 92 | 93 | def print_outcome(x, o): 94 | win_status(x) 95 | win_status(o) 96 | if win_status(x) and win_status(o): 97 | print("Impossible") 98 | elif too_many(x, o) is True: 99 | print("Impossible") 100 | elif win_status(x): 101 | print(f"{x} wins") 102 | elif win_status(o): 103 | print(f"{o} wins") 104 | elif win_status(x) is not True and win_status(o) is not True: 105 | if finished(game_board) is True: 106 | print("Game not finished") 107 | else: 108 | print("Draw") 109 | 110 | print_outcome(player_1, player_2) 111 | 112 | 113 | print_field(user_field) 114 | print_game_status() -------------------------------------------------------------------------------- /Tic-Tac-Toe/tictactoe.py: -------------------------------------------------------------------------------- 1 | # write your code here 2 | user_field = "_" * 9 3 | game_board = [] 4 | game_status = [] 5 | player_1 = "X" 6 | player_2 = "O" 7 | 8 | 9 | def print_field(field, current_player): 10 | global game_board 11 | global user_field 12 | coordinates = list() 13 | game_board = [[field[0], field[1], field[2]], 14 | [field[3], field[4], field[5]], 15 | [field[6], field[7], field[8]]] 16 | 17 | def printer(board): 18 | print("---------") 19 | print("| " + " ".join(board[0]) + " |") 20 | print("| " + " ".join(board[1]) + " |") 21 | print("| " + " ".join(board[2]) + " |") 22 | print("---------") 23 | 24 | if user_field == "_" * 9: 25 | printer(game_board) # print before input only if board is empty 26 | 27 | field = input("Enter the coordinates: ").split() 28 | 29 | def validation(user_input): 30 | # validate input type 31 | for item in user_input: 32 | if item.isnumeric(): 33 | if 1 <= int(item) <= 3: 34 | # - 1 to match matrix coordinate (0-2 vs 1-3) 35 | coordinates.append(int(item) - 1) 36 | else: 37 | print("Coordinates should be from 1 to 3!") 38 | coordinates.clear() 39 | return False 40 | else: 41 | print("You should enter numbers!") 42 | coordinates.clear() 43 | return False 44 | del coordinates[2:] 45 | # adjust coordinates for matrix output 46 | coordinates[1] = abs(coordinates[1] - 2) 47 | coordinates.reverse() 48 | if game_board[coordinates[0]][coordinates[1]] == "_": 49 | # trim extra input items if coordinate list is too long 50 | return coordinates, True 51 | else: 52 | print("This cell is occupied! Choose another one!") 53 | coordinates.clear() 54 | return False 55 | 56 | while validation(field) is False: 57 | field = input("Enter the coordinates: ").split() 58 | 59 | # update game board with new move 60 | game_board[coordinates[0]][coordinates[1]] = current_player 61 | printer(game_board) 62 | user_field = "".join(game_board[0]) + "".join(game_board[1]) + "".join(game_board[2]) 63 | return field, user_field 64 | 65 | 66 | def print_game_status(): 67 | global game_status 68 | 69 | def win_status(player): 70 | # check rows 71 | for row in game_board: 72 | if row.count(player) == 3: 73 | return True 74 | # check columns 75 | column = [[], [], []] 76 | for x in range(0, 3): 77 | for row in game_board: 78 | column[x].append(row[x]) 79 | if column[x].count(player) == 3: 80 | return True 81 | # check diagonals 82 | diagonal_1 = [game_board[0][0], game_board[1][1], game_board[2][2]] 83 | diagonal_2 = [game_board[0][2], game_board[1][1], game_board[2][0]] 84 | if diagonal_1.count(player) == 3 or diagonal_2.count(player) == 3: 85 | return True 86 | 87 | def finished(board): 88 | for i in range(len(board)): 89 | if "_" in board[i]: 90 | return False 91 | 92 | def too_many(x, o): 93 | num_x = [square for row in game_board for square in row if square == x] 94 | num_y = [square for row in game_board for square in row if square == o] 95 | if abs(len(num_x) - len(num_y)) >= 2: 96 | return True 97 | 98 | def print_outcome(x, o): 99 | win_status(x) 100 | win_status(o) 101 | if win_status(x) and win_status(o): 102 | print("Impossible") 103 | elif too_many(x, o) is True: 104 | print("Impossible") 105 | elif win_status(x): 106 | print(f"{x} wins") 107 | elif win_status(o): 108 | print(f"{o} wins") 109 | elif win_status(x) is not True and win_status(o) is not True: 110 | if finished(game_board) is False: 111 | # print("Game not finished") 112 | pass 113 | else: 114 | print("Draw") 115 | game_status.append(True) 116 | 117 | game_status = [win_status(player_1), win_status(player_2), 118 | finished(game_board), too_many(player_1, player_2)] 119 | print_outcome(player_1, player_2) 120 | return game_status 121 | 122 | 123 | def make_move(player): 124 | print_field(user_field, player) 125 | print_game_status() 126 | 127 | 128 | while any(game_status) is not True: 129 | make_move(player_1) 130 | if any(game_status) is not True: 131 | make_move(player_2) -------------------------------------------------------------------------------- /Zookeeper/zookeeper_part3.py: -------------------------------------------------------------------------------- 1 | camel = """ 2 | Switching on camera from habitat with camels... 3 | ___.-''''-. 4 | /___ @ | 5 | ',,,,. | _.'''''''._ 6 | ' | / \\ 7 | | \ _.-' \\ 8 | | '.-' '-. 9 | | ', 10 | | '', 11 | ',,-, ':; 12 | ',,| ;,, ,' ;; 13 | ! ; !'',,,',',,,,'! ; ;: 14 | : ; ! ! ! ! ; ; :; 15 | ; ; ! ! ! ! ; ; ;, 16 | ; ; ! ! ! ! ; ; 17 | ; ; ! ! ! ! ; ; 18 | ;,, !,! !,! ;,; 19 | /_I L_I L_I /_I 20 | Yey, our little camel is sunbathing!""" 21 | 22 | lion = """ 23 | Switching on camera from habitat with lions... 24 | ,w. 25 | ,YWMMw ,M , 26 | _.---.._ __..---._.'MMMMMw,wMWmW, 27 | _.-"" ''' YP"WMMMMMMMMMb, 28 | .-' __.' .' MMMMW^WMMMM; 29 | _, .'.-'"; `, /` .--"" :MMM[==MWMW^; 30 | ,mM^" ,-'.' / ; ; / , MMMMb_wMW" @\\ 31 | ,MM:. .'.-' .' ; `\ ; `, MMMMMMMW `"=./`-, 32 | WMMm__,-'.' / _.\ F'''-+,, ;_,_.dMMMMMMMM[,_ / `=_} 33 | "^MP__.-' ,-' _.--"" `-, ; \ ; ;MMMMMMMMMMW^``; __| 34 | / .' ; ; ) )`{ \ `"^W^`, \ : 35 | / .' / ( .' / Ww._ `. `" 36 | / Y, `, `-,=,_{ ; MMMP`""-, `-._.-, 37 | (--, ) `,_ / `) \/"") ^" `-, -;"\: 38 | The lion is croaking!""" 39 | 40 | deer = """ 41 | Switching on camera from habitat with deers... 42 | /| |\\ 43 | `__\\\\ //__' 44 | || || 45 | \__`\ |'__/ 46 | `_\\\\ //_' 47 | _.,:---;,._ 48 | \_: :_/ 49 | |@. .@| 50 | | | 51 | ,\.-./ \\ 52 | ;;`-' `---__________-----.-. 53 | ;;; \_\\ 54 | ';;; | 55 | ; | ; 56 | \ \ \ | / 57 | \_, \ / \ |\\ 58 | |';| |,,,,,,,,/ \ \ \_ 59 | | | | \ / | 60 | \ \ | | / \ | 61 | | || | | | | | 62 | | || | | | | | 63 | | || | | | | | 64 | |_||_| |_| |_| 65 | /_//_/ /_/ /_/ 66 | Our 'Bambi' looks hungry. Let's go to feed it!""" 67 | 68 | goose = """ 69 | Switching on camera from habitat with lovely goose... 70 | 71 | _ 72 | ,-"" "". 73 | ,' ____ `. 74 | ,' ,' `. `._ 75 | (`. _..--.._ ,' ,' \ \\ 76 | (`-.\ .-"" ""' / ( d _b 77 | (`._ `-"" ,._ ( `-( \\ 78 | <_ ` ( <`< \ `-._\\ 79 | <`- (__< < : 80 | (__ (_<_< ; 81 | `------------------------------------------ 82 | This bird stares intently at you... (Maybe it's time to change the channel?)""" 83 | 84 | bat = """ 85 | Switching on camera from habitat with bats... 86 | _________________ _________________ 87 | ~-. \ |\___/| / .-~ 88 | ~-. \ / o o \ / .-~ 89 | > \\\\ W // < 90 | / /~---~\ \\ 91 | /_ | | _\\ 92 | ~-. | | .-~ 93 | ; \ / i 94 | /___ /\ /\ ___\\ 95 | ~-. / \_/ \ .-~ 96 | V V 97 | It looks like this bat is fine.""" 98 | 99 | rabbit = """ 100 | Switching on camera from habitat with rabbits... 101 | , 102 | /| __ 103 | / | ,-~ / 104 | Y :| // / 105 | | jj /( .^ 106 | >-"~"-v" 107 | / Y 108 | jo o | 109 | ( ~T~ j 110 | >._-' _./ 111 | / "~" | 112 | Y _, | 113 | /| ;-"~ _ l 114 | / l/ ,-"~ \\ 115 | \//\/ .- \\ 116 | Y / Y 117 | l I ! 118 | ]\ _\ /"\\ 119 | (" ~----( ~ Y. ) 120 | It seems there will be more rabbits soon!""" 121 | 122 | # write your code here 123 | zoo = [camel, lion, deer, goose, bat, rabbit] 124 | print("Which habitat # do you need?") 125 | habitat = int(input()) 126 | print(zoo[habitat]) 127 | print("---") 128 | print("The end of the program. To check another habitat restart the watcher please.") -------------------------------------------------------------------------------- /Zookeeper/zookeeper.py: -------------------------------------------------------------------------------- 1 | camel = """ 2 | Switching on camera from habitat with camels... 3 | ___.-''''-. 4 | /___ @ | 5 | ',,,,. | _.'''''''._ 6 | ' | / \\ 7 | | \ _.-' \\ 8 | | '.-' '-. 9 | | ', 10 | | '', 11 | ',,-, ':; 12 | ',,| ;,, ,' ;; 13 | ! ; !'',,,',',,,,'! ; ;: 14 | : ; ! ! ! ! ; ; :; 15 | ; ; ! ! ! ! ; ; ;, 16 | ; ; ! ! ! ! ; ; 17 | ; ; ! ! ! ! ; ; 18 | ;,, !,! !,! ;,; 19 | /_I L_I L_I /_I 20 | Yey, our little camel is sunbathing!""" 21 | 22 | lion = """ 23 | Switching on camera from habitat with lions... 24 | ,w. 25 | ,YWMMw ,M , 26 | _.---.._ __..---._.'MMMMMw,wMWmW, 27 | _.-"" ''' YP"WMMMMMMMMMb, 28 | .-' __.' .' MMMMW^WMMMM; 29 | _, .'.-'"; `, /` .--"" :MMM[==MWMW^; 30 | ,mM^" ,-'.' / ; ; / , MMMMb_wMW" @\\ 31 | ,MM:. .'.-' .' ; `\ ; `, MMMMMMMW `"=./`-, 32 | WMMm__,-'.' / _.\ F'''-+,, ;_,_.dMMMMMMMM[,_ / `=_} 33 | "^MP__.-' ,-' _.--"" `-, ; \ ; ;MMMMMMMMMMW^``; __| 34 | / .' ; ; ) )`{ \ `"^W^`, \ : 35 | / .' / ( .' / Ww._ `. `" 36 | / Y, `, `-,=,_{ ; MMMP`""-, `-._.-, 37 | (--, ) `,_ / `) \/"") ^" `-, -;"\: 38 | The lion is croaking!""" 39 | 40 | deer = """ 41 | Switching on camera from habitat with deers... 42 | /| |\\ 43 | `__\\\\ //__' 44 | || || 45 | \__`\ |'__/ 46 | `_\\\\ //_' 47 | _.,:---;,._ 48 | \_: :_/ 49 | |@. .@| 50 | | | 51 | ,\.-./ \\ 52 | ;;`-' `---__________-----.-. 53 | ;;; \_\\ 54 | ';;; | 55 | ; | ; 56 | \ \ \ | / 57 | \_, \ / \ |\\ 58 | |';| |,,,,,,,,/ \ \ \_ 59 | | | | \ / | 60 | \ \ | | / \ | 61 | | || | | | | | 62 | | || | | | | | 63 | | || | | | | | 64 | |_||_| |_| |_| 65 | /_//_/ /_/ /_/ 66 | Our 'Bambi' looks hungry. Let's go to feed it!""" 67 | 68 | goose = """ 69 | Switching on camera from habitat with lovely goose... 70 | 71 | _ 72 | ,-"" "". 73 | ,' ____ `. 74 | ,' ,' `. `._ 75 | (`. _..--.._ ,' ,' \ \\ 76 | (`-.\ .-"" ""' / ( d _b 77 | (`._ `-"" ,._ ( `-( \\ 78 | <_ ` ( <`< \ `-._\\ 79 | <`- (__< < : 80 | (__ (_<_< ; 81 | `------------------------------------------ 82 | This bird stares intently at you... (Maybe it's time to change the channel?)""" 83 | 84 | bat = """ 85 | Switching on camera from habitat with bats... 86 | _________________ _________________ 87 | ~-. \ |\___/| / .-~ 88 | ~-. \ / o o \ / .-~ 89 | > \\\\ W // < 90 | / /~---~\ \\ 91 | /_ | | _\\ 92 | ~-. | | .-~ 93 | ; \ / i 94 | /___ /\ /\ ___\\ 95 | ~-. / \_/ \ .-~ 96 | V V 97 | It looks like this bat is fine.""" 98 | 99 | rabbit = """ 100 | Switching on camera from habitat with rabbits... 101 | , 102 | /| __ 103 | / | ,-~ / 104 | Y :| // / 105 | | jj /( .^ 106 | >-"~"-v" 107 | / Y 108 | jo o | 109 | ( ~T~ j 110 | >._-' _./ 111 | / "~" | 112 | Y _, | 113 | /| ;-"~ _ l 114 | / l/ ,-"~ \\ 115 | \//\/ .- \\ 116 | Y / Y 117 | l I ! 118 | ]\ _\ /"\\ 119 | (" ~----( ~ Y. ) 120 | It seems there will be more rabbits soon!""" 121 | 122 | animals = [camel, lion, deer, goose, bat, rabbit] 123 | 124 | # write your code here 125 | print("Which habitat # do you need?") 126 | 127 | habitat = input() 128 | while habitat != "exit": 129 | print(animals[int(habitat)]) 130 | print("Which habitat # do you need?") 131 | habitat = input() 132 | 133 | print("See you!") -------------------------------------------------------------------------------- /sample_python_code.py: -------------------------------------------------------------------------------- 1 | # Python → Introduction to Python → Introduction to Python 2 | print("Learn Python to be great!") 3 | 4 | # Python → Introduction to Python → Overview of the basic program 5 | ''' 6 | Code Challenge — Write a program 7 | 8 | Write a program that prints the string "My name is " followed by your name like in the output example. All words in this string must be separated by a single space. 9 | 10 | Output example 11 | 12 | My name is John 13 | ''' 14 | print("My name is John") 15 | 16 | 17 | ''' 18 | 19 | The title of Alice's Adventures in Wonderland is often shortened to Alice in Wonderland. 20 | 21 | Now, when we know how to print strings with quotes, we won't shorten it. Please print the full book title. 22 | ''' 23 | print("Alice's Adventures in Wonderland ") 24 | 25 | ''' 26 | 27 | Write a program that prints the following line. 28 | 29 | 2 + 2 = 4 30 | ''' 31 | print("2 + 2 = 4") 32 | 33 | ''' 34 | 35 | Write a program that prints out a string We learn Python! 36 | 37 | Note that this task is case-sensitive. 38 | ''' 39 | print("We learn Python!") 40 | 41 | # Python → Introduction to Python → Multi-line programs 42 | 43 | ''' 44 | Write a program that prints the following three text lines in the column. 45 | 46 | first 47 | second 48 | third 49 | ''' 50 | print("first") 51 | print("second") 52 | print("third") 53 | 54 | ''' 55 | 56 | You need to write a program to display the result grid of a game in Tic-Tac-Toe. 57 | 58 | The grid is shown below. 59 | 60 | O X X 61 | O X O 62 | X O X 63 | 64 | Note: O is not a zero, it is a letter. The letters X and O should be uppercase. 65 | ''' 66 | print("O X X") 67 | print("O X O") 68 | print("X O X") 69 | 70 | ''' 71 | 72 | Write a program that prints this square out of * symbols. 73 | 74 | * * * * 75 | * * 76 | * * 77 | * * * * 78 | 79 | Don't forget about whitespaces! 80 | ''' 81 | print("* * * *") 82 | print("* *") 83 | print("* *") 84 | print("* * * *") 85 | 86 | ''' 87 | 88 | Write a program that prints the following text: 89 | 90 | WE NEED 91 | 92 | TO LEARN PYTHON 93 | 94 | AS QUICKLY AS POSSIBLE 95 | ''' 96 | print("WE NEED") 97 | print("") 98 | print("TO LEARN PYTHON") 99 | print("") 100 | print("AS QUICKLY AS POSSIBLE") 101 | 102 | # Python → Code style → PEP 8 103 | 104 | # Python → Code style → Comments 105 | ''' 106 | 107 | There are several comments "hidden" in the code below. Find them and make sure to comment them so that the code runs correctly. 108 | ''' 109 | # create a variable x 110 | # with the value 8 111 | x = 8 112 | x = x * x 113 | print(x) # prints the x squared 114 | 115 | ''' 116 | Given a line of code and comment, please combine them and write them down as they would look like in a real program. Please make a comment an inline one. 117 | 118 | Code: word = word.replace("\u0301", "") 119 | 120 | Comment: delete stress symbols from the word 121 | ''' 122 | word = word.replace("\u0301", "") # delete stress symbols from the word 123 | 124 | 125 | ''' 126 | 127 | It is sometimes useful to comment out some parts of a program if you don't need them now and want to save them for later. Look at the following code, copy it to the test and decide what line to comment out so that the code will print an integer 7. 128 | 129 | print(1 + 2 + 3 + 6) 130 | print(1 + 3 + 3) 131 | print(1 + 2 + 3) 132 | ''' 133 | # print(1 + 2 + 3 + 6) 134 | print(1 + 3 + 3) 135 | # print(1 + 2 + 3) 136 | 137 | ''' 138 | 139 | Write the code that corresponds to the comment. 140 | ''' 141 | # prints "ok" 142 | print("ok") 143 | 144 | # Python → Data types and operations → Basic data types 145 | 146 | ''' 147 | 148 | Print an int with a value 10. 149 | 150 | Sample Input 1: 151 | 152 | Sample Output 1: 153 | 154 | 10 155 | ''' 156 | print(10) 157 | 158 | ''' 159 | 160 | Print the numbers from 1 to 10 in a single line: 1 2 3 4 5 6 7 8 9 10. 161 | ''' 162 | print("1 2 3 4 5 6 7 8 9 10") 163 | 164 | ''' 165 | 166 | Write the code that prints the type of 3 objects that are written below (in this exact order). 167 | 168 | "int" 169 | 394 170 | 2.71 171 | ''' 172 | print(type("int")) 173 | print(type(394)) 174 | print(type(2.71)) 175 | 176 | ''' 177 | 178 | Try to print a float number 1.0000000000000001. 179 | ''' 180 | print(1.0000000000000001) 181 | 182 | ''' 183 | 184 | Print a string Supercalifragilisticexpialidocious below. 185 | ''' 186 | print("Supercalifragilisticexpialidocious") 187 | 188 | # Python → Data types and operations → Variables 189 | 190 | ''' 191 | 192 | Define a numeric variable with name number and value 10. 193 | ''' 194 | number = 10 195 | 196 | ''' 197 | 198 | Create a variable favorite_holiday with a value "Cinnamon Roll Day" 199 | ''' 200 | favorite_holiday = "Cinnamon Roll Day" 201 | 202 | # Python → Simple programs → Taking input 203 | 204 | ''' 205 | 206 | Let's see how it works. Copy the following code and paste it below. Then change one of the lines so that the program would print any data the user inputs without performing any operations on them. 207 | 208 | data = ... 209 | print(data) 210 | 211 | Sample Input 1: 212 | 213 | 10 214 | 215 | Sample Output 1: 216 | 217 | 10 218 | ''' 219 | # put your python code here 220 | data = input() 221 | print(data) 222 | 223 | ''' 224 | 225 | Write a program that prints the sum of three integer numbers. Read each number from the user separately. 226 | 227 | Sample Input 1: 228 | 229 | 3 230 | 6 231 | 8 232 | 233 | Sample Output 1: 234 | 235 | 17 236 | ''' 237 | # put your python code here 238 | num1 = int(input()) 239 | num2 = int(input()) 240 | num3 = int(input()) 241 | print(num1 + num2 + num3) 242 | 243 | ''' 244 | 245 | Imagine you are writing a program that will give a prediction for users. And imagine you intend to do so based on a number they enter. Write a program that reads a three-digit number and prints the sum of its digits. 246 | 247 | Input: positive integer 248 | 249 | Output: positive integer 250 | 251 | Sample Input 1: 252 | 253 | 672 254 | 255 | Sample Output 1: 256 | 257 | 15 258 | ''' 259 | # put your code here 260 | number = input() 261 | print(int(number[0]) + int(number[1]) + int(number[2])) 262 | 263 | ''' 264 | 265 | Ask the user about parameters of a rectangular parallelepiped (3 integers representing the length, width and height) and print the sum of edge lengths, its total surface area and volume. 266 | 267 | Sum of lengths of all edges: 268 | 269 | s=4(a+b+c)s = 4(a + b + c)s=4(a+b+c) 270 | 271 | Surface area: 272 | 273 | S=2(ab+bc+ac)S = 2(ab + bc + ac)S=2(ab+bc+ac) 274 | 275 | Volume: 276 | 277 | V=abcV = abcV=abc 278 | 279 | Sample Input 1: 280 | 281 | 5 282 | 10 283 | 15 284 | 285 | Sample Output 1: 286 | 287 | 120 288 | 550 289 | 750 290 | 291 | ''' 292 | # put your python code here 293 | length = int(input()) 294 | width = int(input()) 295 | height = int(input()) 296 | 297 | print(4 * (length + width + height)) 298 | print(2 * (length * width + width * height + length * height)) 299 | print(length * width * height) 300 | 301 | ''' 302 | 303 | Write a program that reads the input from the user and then prints "Hello, " + user input. Don't print any additional messages! 304 | 305 | Sample Input 1: 306 | 307 | Sauron 308 | 309 | Sample Output 1: 310 | 311 | Hello, Sauron 312 | ''' 313 | name = input() 314 | print("Hello, " + name) 315 | 316 | ''' 317 | 318 | Write a program that reads two integer numbers from user and then prints their difference. 319 | 320 | Sample Input 1: 321 | 322 | 8 323 | 11 324 | 325 | Sample Output 1: 326 | 327 | -3 328 | ''' 329 | num1 = int(input()) 330 | num2 = int(input()) 331 | print(num1 - num2) 332 | 333 | ''' 334 | 335 | Write a program that reads two float numbers from the user (use the float()) and prints their sum. 336 | 337 | Sample Input 1: 338 | 339 | 8.77 340 | 11.25 341 | 342 | Sample Output 1: 343 | 344 | 20.02 345 | ''' 346 | num1 = float(input()) 347 | num2 = float(input()) 348 | print(num1 + num2) 349 | 350 | ''' 351 | 352 | Have you ever dreamed of becoming a songwriter? It's time to make a hit. We will leave a verse for later and write the chorus part instead. 353 | 354 | All you need to do is to read the input number n and an input word (they are given on separate lines) and to repeat this word exactly n times. 355 | 356 | Finally, print your song for us, please! 357 | 358 | Sample Input 1: 359 | 360 | 7 361 | la 362 | 363 | Sample Output 1: 364 | 365 | lalalalalalala 366 | ''' 367 | print(int(input()) * input()) 368 | 369 | # Python → Data types and operations → Integer arithmetic 370 | 371 | ''' 372 | 373 | Write a program that calculates a remainder of 10 divided by 3 and prints the result 374 | ''' 375 | # put your python code here 376 | print(10 % 3) 377 | 378 | ''' 379 | 380 | Please, print() the result of the following operations: first raise 31 into the power of 331; get the remainder of the result's division by 20. 381 | ''' 382 | print((31 ** 331) % 20) 383 | 384 | ''' 385 | 386 | Print the result of execution of the following expression 1234567890 multiplied by 987654321 then add 67890 387 | ''' 388 | # put your python code here 389 | print(1234567890 * 987654321 + 67890) 390 | 391 | ''' 392 | 393 | Write a program that prints the product of these three numbers 1 * 2 * 3. 394 | ''' 395 | print(1 * 2 * 3) 396 | 397 | ''' 398 | 399 | Write a program that takes two integer numbers a and b and prints their sum. 400 | 401 | The variables have already been defined. 402 | 403 | Sample Input 1: 404 | 405 | 8 406 | 11 407 | 408 | Sample Output 1: 409 | 410 | 19 411 | ''' 412 | a = int(input()) 413 | b = int(input()) 414 | 415 | # calculate the sum below 416 | print(a + b) 417 | 418 | ''' 419 | 420 | Write a program that takes 3 integer numbers a, b and c, subtracts c from a multiplied by b and then prints the result. 421 | 422 | There's no need to create the variables, just use them in the code below. 423 | 424 | Sample Input 1: 425 | 426 | 8 427 | 11 428 | 63 429 | 430 | Sample Output 1: 431 | 432 | 25 433 | ''' 434 | # work with these variables 435 | a = int(input()) 436 | b = int(input()) 437 | c = int(input()) 438 | 439 | print(a * b - c) 440 | 441 | ''' 442 | 443 | Write a program that takes a single integer number n and then performs the following operations in the following order: 444 | 445 | adds n to itself 446 | multiplies the result by n 447 | subtracts n from the result 448 | exactly divides the result by n (that means, you need to carry out integer division). 449 | 450 | Then print the result of the division. The example is given below: 451 | 452 | 8 + 8 = 16 453 | 16 * 8 = 128 454 | 128 - 8 = 120 455 | 120 // 8 = 15 456 | 457 | The variable n is already defined. 458 | 459 | Sample Input 1: 460 | 461 | 8 462 | 463 | Sample Output 1: 464 | 465 | 15 466 | ''' 467 | 468 | n = int(input()) 469 | print((((n + n) * n) - n) // n) 470 | 471 | ''' 472 | 473 | It's time for really big numbers! Calculate this 2^{179} and print what you got. 474 | ''' 475 | print(2 ** 179) 476 | 477 | # 478 | 479 | # ============================================================================= 480 | print('Hello! My name is Aid.') 481 | print('I was created in 2020.') 482 | print('Please, remind me your name.') 483 | 484 | # reading a name 485 | name = input() 486 | print('What a great name you have, ' + name + '!') 487 | # ============================================================================= 488 | 489 | # Python → Code style → Naming variables 490 | 491 | ''' 492 | Code Challenge — Write a program 493 | Here's a list of variables: 494 | 495 | n = 10 496 | model_score = 0.9875 497 | client_name = "Bob" 498 | colorOfTheShirt = "red" 499 | Some of them are named according to naming conventions and best practices, others are not. 500 | 501 | Copy only those variables that have good names in your code. You don't need to use the print() function. 502 | ''' 503 | # copy the correct variables here 504 | model_score = 0.9875 505 | client_name = "Bob" 506 | 507 | ''' 508 | Code Challenge — Write a program 509 | The previous developer accidentally overrode the str method and now the code fails. 510 | 511 | Please fix the variable by adding "_" to the end of this name as it's said in the code style convention. 512 | Your output should look like Hello10 and you don't need to use print. 513 | ''' 514 | str_ = "Hello" 515 | str_ = str_ + str(10) 516 | 517 | ''' 518 | Code Challenge — Write a program 519 | Rename the variables into the code below according to the Python code style convention. 520 | 521 | httpResponse = 'mocked response' 522 | httpError = 404 523 | ''' 524 | http_response = 'mocked response' 525 | http_error = 404 526 | 527 | # Python → Simple programs → Program with numbers 528 | 529 | ''' 530 | Code Challenge — Write a program 531 | Write a program that reads an integer value n from the standard input and prints the result of the expression: 532 | 533 | ((n + 1) * n + 2) * n + 3 534 | Sample Input 1: 535 | 536 | 3 537 | Sample Output 1: 538 | 539 | 45 540 | ''' 541 | # put your python code here 542 | n = int(input()) 543 | print(((n + 1) * n + 2) * n + 3) 544 | 545 | 546 | ''' 547 | Code Challenge — Write a program 548 | Lucky tickets are a kind of mathematical entertainment. A ticket is considered lucky if the sum of the first 3 digits coincides with the sum of the last 3 digits of the ticket number. 549 | You are supposed to write a program that checks whether the two sums are equal. The code snippet below displays "Lucky" if they do and "Ordinary" otherwise. 550 | 551 | However, some parts of the code are missing. Fill in the blanks to make it work! 552 | 553 | Input: a string of 6 digits. 554 | 555 | Output: either "Lucky" or "Ordinary" (without quotes). 556 | 557 | Hint 558 | 559 | Sample Input 1: 560 | 561 | 090234 562 | Sample Output 1: 563 | 564 | Lucky 565 | Sample Input 2: 566 | 567 | 123456 568 | Sample Output 2: 569 | 570 | Ordinary 571 | ''' 572 | # Save the input in this variable 573 | ticket = int(input()) 574 | 575 | # Add up the digits for each half 576 | half1 = ticket // 100000 + ticket // 10000 % 10 + ticket // 1000 % 10 577 | half2 = ticket % 1000 // 100 + ticket % 100 // 10 + ticket % 10 578 | 579 | # Thanks to you, this code will work 580 | if half1 == half2: 581 | print("Lucky") 582 | else: 583 | print("Ordinary") 584 | 585 | ''' 586 | Code Challenge — Write a program 587 | Some school have decided to create three new math groups and equip classrooms for them with the new desks. Only two students may sit at any desk. The number of students in each of the three groups is known. Output the smallest amount of desks, which will need to be purchased. Each new group sits in its own classroom. 588 | 589 | Input data format 590 | 591 | The program receives the input of the three non-negative integers: the number of students in each of the three classes (the numbers do not exceed 1000). 592 | 593 | Sample Input 1: 594 | 595 | 20 596 | 21 597 | 22 598 | Sample Output 1: 599 | 600 | 32 601 | Sample Input 2: 602 | 603 | 16 604 | 18 605 | 20 606 | Sample Output 2: 607 | 608 | 27 609 | ''' 610 | # put your python code here 611 | num1 = int(input()) 612 | num2 = int(input()) 613 | num3 = int(input()) 614 | print(num1 % 2 + num1 // 2 + num2 % 2 + num2 // 2 + num3 % 2 + num3 // 2) 615 | 616 | ''' 617 | Code Challenge — Write a program 618 | Given a three-digit integer (i.e. an integer from 100 to 999). Find the sum of its digits. 619 | 620 | Sample Input 1: 621 | 622 | 476 623 | Sample Output 1: 624 | 625 | 17 626 | ''' 627 | # put your python code here 628 | num = int(input()) 629 | first_digit = num // 100 630 | second_digit = num // 10 % 10 631 | third_digit = num % 10 632 | print(first_digit + second_digit + third_digit) 633 | 634 | ''' 635 | Code Challenge — Write a program 636 | Write a program that reads an integer and outputs its last digit. 637 | 638 | Sample Input 1: 639 | 640 | 425 641 | Sample Output 1: 642 | 643 | 5 644 | ''' 645 | # put your python code here 646 | print(int(input()) % 10) 647 | 648 | ''' 649 | Code Challenge — Write a program 650 | Given a two-digit number. Output its first digit (i.e. the number of tens). 651 | 652 | Sample Input 1: 653 | 654 | 42 655 | Sample Output 1: 656 | 657 | 4 658 | ''' 659 | # put your python code here 660 | print(int(input()) // 10) 661 | 662 | ''' 663 | Code Challenge — Write a program 664 | N squirrels found K nuts and decided to divide them equally. Determine how many nuts each squirrel will get. 665 | 666 | Input data format 667 | 668 | There are two positive numbers N and K, each of them is not greater than 10000. 669 | 670 | Sample Input 1: 671 | 672 | 3 673 | 14 674 | Sample Output 1: 675 | 676 | 4 677 | ''' 678 | # put your python code here 679 | n_squirrels = int(input()) 680 | k_nuts = int(input()) 681 | 682 | print(k_nuts // n_squirrels) 683 | 684 | ''' 685 | Code Challenge — Write a program 686 | N squirrels found K nuts and decided to divide them equally. Find how many nuts will be left after each of the squirrels takes the equal amount of nuts. 687 | 688 | Input data format 689 | 690 | There are two positive integers N and K, each of them is not greater than 10000. 691 | 692 | Sample Input 1: 693 | 694 | 3 695 | 14 696 | Sample Output 1: 697 | 698 | 2 699 | ''' 700 | # put your python code here 701 | n_squirrels = int(input()) 702 | k_nuts = int(input()) 703 | 704 | print(k_nuts % n_squirrels) 705 | 706 | ''' 707 | Code Challenge — Write a program 708 | Calculate and print the income from a saving account with a 5% interest rate after a year for a given amount. 709 | 710 | Sample Input 1: 711 | 712 | None 713 | Sample Output 1: 714 | 715 | 50.0 716 | ''' 717 | amount = 1000 718 | interest_rate = 5 719 | years = 1 720 | 721 | income = years * interest_rate / 100 * amount 722 | 723 | print(income) 724 | 725 | ''' 726 | Code Challenge — Write a program 727 | You will get the values for two moments in time of the same day: hours, minutes and seconds for each time point. It is known that the first point of time happened earlier than the second one. Find out how many seconds passed between these two time instants. 728 | 729 | Input data format 730 | 731 | The program gets the input of three integers, hours, minutes, seconds, defining the first moment of time and three integers that define similarly the second moment. 732 | 733 | Output data format 734 | 735 | Output the number of seconds between these two moments of time. 736 | 737 | Sample Input 1: 738 | 739 | 1 740 | 1 741 | 1 742 | 2 743 | 2 744 | 2 745 | Sample Output 1: 746 | 747 | 3661 748 | Sample Input 2: 749 | 750 | 1 751 | 2 752 | 30 753 | 1 754 | 3 755 | 20 756 | Sample Output 2: 757 | 758 | 50 759 | ''' 760 | # put your python code here 761 | first_hour = int(input()) 762 | first_minute = int(input()) 763 | first_second = int(input()) 764 | second_hour = int(input()) 765 | second_minute = int(input()) 766 | second_second = int(input()) 767 | 768 | first = first_hour * 60 * 60 + first_minute * 60 + first_second 769 | second = second_hour * 60 * 60 + second_minute * 60 + second_second 770 | 771 | print(second - first) 772 | 773 | ''' 774 | Code Challenge — Write a program 775 | Write a program that will help people who are going on vacation. The program should calculate the total required sum (e.g. $) of money to have a good rest for a given duration. 776 | 777 | There are four parameters which have to be considered: 778 | 779 | duration in days 780 | total food cost per a day 781 | one-way flight cost 782 | cost of one night in a hotel (the number of nights equal duration minus one) 783 | Read values of these parameters from the standard input and then print the result. 784 | 785 | Hint 786 | 787 | Sample Input 1: 788 | 789 | 7 790 | 30 791 | 100 792 | 40 793 | Sample Output 1: 794 | 795 | 650 796 | ''' 797 | # put your python code here 798 | duration = int(input()) 799 | daily_food_cost = int(input()) 800 | one_way_flight = int(input()) 801 | daily_hotel_rate = int(input()) 802 | 803 | print(duration 804 | * daily_food_cost + 2 805 | * one_way_flight 806 | + (duration - 1) 807 | * daily_hotel_rate) 808 | 809 | # Python → Data types and operations → Membership testing 810 | 811 | ''' 812 | Code Challenge — Write a program 813 | Fill the value of the substring so that the membership test would result in True regardless of test_string value. 814 | 815 | The variable for the test_string has already been defined. 816 | 817 | Sample Input 1: 818 | 819 | reMEMBER my name 820 | Sample Output 1: 821 | 822 | True 823 | ''' 824 | test_string = input() 825 | # fill the value of the variable below 826 | substring = '' 827 | print(substring in test_string) 828 | 829 | ''' 830 | Code Challenge — Write a program 831 | Write a program that takes two strings, checks whether the first string contains the second one and prints the result of the membership test. 832 | 833 | The variables for both input strings have already been defined. 834 | 835 | Sample Input 1: 836 | 837 | Big Brother watches you 838 | Father 839 | Sample Output 1: 840 | 841 | False 842 | ''' 843 | a = input() 844 | b = input() 845 | print(b in a) 846 | 847 | # Python → Data types and operations → Comparisons 848 | 849 | ''' 850 | Code Challenge — Write a program 851 | Write a program that reads an integer value from the input and checks if it is positive. 852 | 853 | Hints: 854 | 855 | 0 is not a positive number. 856 | 857 | A comparison already returns a boolean, so if you need the result of the comparison, you can print it, like this print(5 > 9) # False 858 | 859 | Sample Input 1: 860 | 861 | 8 862 | Sample Output 1: 863 | 864 | True 865 | ''' 866 | # don't modify this code 867 | # a stores an input value 868 | a = int(input().strip()) 869 | 870 | # put your code here 871 | print(a > 0) 872 | 873 | ''' 874 | Code Challenge — Write a program 875 | The movie theater has N cinema halls that can accommodate K viewers each. Figure out if a movie theater can hold V viewers that plan to visit it on a particular day. 876 | 877 | The input format 878 | 879 | The first line is N cinema halls, the second line is their capacity (K), and the third line is the planned number of viewers (V). 880 | 881 | The output format 882 | 883 | True or False. 884 | 885 | Sample Input 1: 886 | 887 | 9 888 | 68 889 | 589 890 | Sample Output 1: 891 | 892 | True 893 | ''' 894 | # put your python code here 895 | cinema_halls = int(input()) 896 | capacity = int(input()) 897 | num_of_viewers = int(input()) 898 | 899 | print(num_of_viewers <= cinema_halls * capacity ) 900 | 901 | ''' 902 | Code Challenge — Write a program 903 | Find out if the result of dividing A by B is an odd number. 904 | 905 | The input format: 906 | 907 | The first line is the dividend (A) and the second line is the divider (B). It is guaranteed that the numbers are divided without remainder. 908 | 909 | The output format: 910 | 911 | True or False 912 | 913 | Sample Input 1: 914 | 915 | 99 916 | 3 917 | Sample Output 1: 918 | 919 | True 920 | ''' 921 | # put your python code here 922 | a = int(input()) 923 | b = int(input()) 924 | 925 | print(a % b == 0) 926 | 927 | ''' 928 | Code Challenge — Write a program 929 | Write a program that reads an integer value from input and checks if it is less than 10 or greater than 250. 930 | 931 | Sample Input 1: 932 | 933 | 0 934 | Sample Output 1: 935 | 936 | True 937 | ''' 938 | # don't modify this code 939 | # a stores an input value 940 | a = int(input().strip()) 941 | 942 | # put your code here 943 | print(a < 10 or a > 250) 944 | 945 | ''' 946 | Code Challenge — Write a program 947 | You are given 3 large numbers, check that they are given in a strictly ascending order. 948 | 949 | The input format: 950 | 951 | Three lines with three numbers. 952 | 953 | The output format: 954 | 955 | True or False 956 | 957 | Sample Input 1: 958 | 959 | 7790765547678990 960 | 7790765557679990 961 | 7790765557778900 962 | Sample Output 1: 963 | 964 | True 965 | ''' 966 | 967 | a = int(input()) 968 | b = int(input()) 969 | c = int(input()) 970 | 971 | print(a < b and b < c and a < c) 972 | 973 | ''' 974 | Code Challenge — Write a program 975 | You are playing a guessing game with a user. Imagine that you came up with an integer stored in a variable set_number. 976 | 977 | Check if set_number is equal to the product of two integers entered by the user. 978 | 979 | The input format: 980 | 981 | Two lines containing integer numbers. 982 | 983 | The output format: 984 | 985 | True if the user guessed correctly and False otherwise. 986 | 987 | Sample Input 1: 988 | 989 | 3 990 | 11 991 | Sample Output 1: 992 | 993 | False 994 | ''' 995 | set_number = 6557 996 | a = int(input()) 997 | b = int(input()) 998 | 999 | print(a * b == set_number) 1000 | 1001 | # Python → Control flow statements → If statement 1002 | 1003 | ''' 1004 | Code Challenge — Write a program 1005 | Any recipe starts with a list of ingredients. Below is an abstract of a cookbook with the ingredients for some dishes. Write a program that tells you what recipe you can make based on the ingredient you have. 1006 | 1007 | The input format: 1008 | 1009 | A name of some ingredient. 1010 | 1011 | The output format: 1012 | 1013 | A message that says You can make and then the name of the dish. For example, You can make pasta. If the ingredient is featured in several recipes, write about all of them in the order they're featured in the cook book. 1014 | 1015 | Sample Input 1: 1016 | 1017 | basil 1018 | Sample Output 1: 1019 | 1020 | You can make pasta 1021 | Sample Input 2: 1022 | 1023 | flour 1024 | Sample Output 2: 1025 | 1026 | You can make apple pie 1027 | You can make chocolate cake 1028 | ''' 1029 | pasta = "tomato, basil, garlic, salt, pasta, olive oil" 1030 | apple_pie = "apple, sugar, salt, cinnamon, flour, egg, butter" 1031 | ratatouille = "aubergine, carrot, onion, tomato, garlic, olive oil, pepper, salt" 1032 | chocolate_cake = "chocolate, sugar, salt, flour, coffee, butter" 1033 | omelette = "egg, milk, bacon, tomato, salt, pepper" 1034 | 1035 | ingredient = input() 1036 | if ingredient in pasta: 1037 | print("You can make pasta") 1038 | if ingredient in apple_pie: 1039 | print("You can make apple pie") 1040 | if ingredient in ratatouille: 1041 | print("You can make ratatouille") 1042 | if ingredient in chocolate_cake: 1043 | print("You can make chocolate cake") 1044 | if ingredient in omelette: 1045 | print("You can make omelette") 1046 | 1047 | ''' 1048 | Code Challenge — Write a program 1049 | Ferryman Charon carries souls across the river to the world of the dead, but does so only for a fee. It's a business after all. 1050 | 1051 | Check whether the recently deceased has a coin, if any, print Welcome to Charon's boat! 1052 | 1053 | The variable coin stores a Boolean value, so it qualifies as a condition. If coin has False value, alas! There's nothing to be done about it. 1054 | 1055 | The variable coin is already defined. So you DON'T need to read its value from the input. 1056 | 1057 | Lastly, let's warn everyone in the underworld (both those in the boat and those overboard) by printing the message There is no turning back. 1058 | ''' 1059 | # the variable `coin` is already defined 1060 | if coin: 1061 | print("Welcome to Charon's boat!") 1062 | print("There is no turning back.") 1063 | 1064 | ''' 1065 | Code Challenge — Write a program 1066 | Alex wrote a program that reads a number and a word from the input to create phrases like "3 cats" and "1 dog". Unfortunately, the condition for plural nouns is currently missing. Alex doesn't know how to use if statements, but you do. Help Alex complete this program. 1067 | 1068 | The plural form of a word generally ends with s. All numbers, apart from 1, expect the plural form after them, even zero: "0 birds". 1069 | 1070 | Sample Input 1: 1071 | 1072 | 1 1073 | engineer 1074 | Sample Output 1: 1075 | 1076 | 1 engineer 1077 | Sample Input 2: 1078 | 1079 | 12 1080 | student 1081 | Sample Output 2: 1082 | 1083 | 12 students 1084 | ''' 1085 | number = int(input()) 1086 | word = input() 1087 | 1088 | # write a condition for plurals 1089 | if number != 1: 1090 | word += "s" 1091 | 1092 | print(number, word) 1093 | 1094 | # Python → Control flow statements → While loop 1095 | 1096 | ''' 1097 | Code Challenge — Write a program 1098 | When a bank has financial problems the government can return a client's deposit if it is less than 700,000. The interest rate for a particular deposit is 7,1% a year. The percentages are paid to the same deposit at the end of the year and a new value of the interest is calculated. 1099 | 1100 | Find out how many years it will take for the sum of the deposit to exceed the value protected by the government. 1101 | 1102 | The input format: 1103 | 1104 | The initial sum of the deposit. It is guaranteed that the value will be between 50,000 and 700,000. 1105 | 1106 | The output format: 1107 | 1108 | The number of years. 1109 | 1110 | Sample Input 1: 1111 | 1112 | 650000 1113 | Sample Output 1: 1114 | 1115 | 2 1116 | ''' 1117 | inital_sum = int(input()) 1118 | counter = 0 1119 | 1120 | while inital_sum < 700000: 1121 | inital_sum *= 1.071 1122 | counter += 1 1123 | 1124 | print(counter) 1125 | 1126 | ''' 1127 | Code Challenge — Write a program 1128 | In nuclear physics, the half-life is used to describe the rate with which elements undergo radioactive decay. More precisely, it is the time required for an element to reduce in half. 1129 | 1130 | Let's take an isotope of Radium (Ra) called radium-223. Its half-life is almost 12 days (11.43, to be exact). Write a program that calculates how many full half-life periods (in days) it would take for a certain amount of radium-223 to reduce to a specific value. 1131 | 1132 | The input format: 1133 | 1134 | The first line with the starting amount of atoms N (from 2 to 1,000,000), the second line with the resulting quantity R. 1135 | 1136 | The output format: 1137 | 1138 | The number of full half-life periods (in days) T it would take for radium-223 to reduce from N to R. That is if it would take at least 1 half-life period, write 12 and so on. 1139 | 1140 | Sample Input 1: 1141 | 1142 | 4 1143 | 3 1144 | Sample Output 1: 1145 | 1146 | 12 1147 | ''' 1148 | n = int(input()) 1149 | r = int(input()) 1150 | counter = 0 1151 | 1152 | while r < n: 1153 | n //= 2 1154 | counter += 1 1155 | 1156 | print(counter * 12) 1157 | 1158 | ''' 1159 | Code Challenge — Write a program 1160 | The program below is supposed to print the squares of all numbers from 1 to 20, but there are some mistakes. Find them and fix them. 1161 | ''' 1162 | 1163 | i = 1 1164 | while i <= 20: 1165 | print(i * i) 1166 | i += 1 1167 | 1168 | ''' 1169 | Code Challenge — Write a program 1170 | Carl asks you to count the sum of the first k natural numbers. Read k from the input, then add up numbers from 1 to k and print your answer. 1171 | 1172 | Sample Input 1: 1173 | 1174 | 18 1175 | Sample Output 1: 1176 | 1177 | 171 1178 | ''' 1179 | 1180 | a = int(input()) 1181 | counter = 1 1182 | total = 0 1183 | 1184 | while counter <= a: 1185 | total += counter 1186 | counter += 1 1187 | print(total) 1188 | 1189 | ''' 1190 | Code Challenge — Write a program 1191 | Write a program that prints all even numbers less than the input number using the while loop. 1192 | 1193 | The input format: 1194 | 1195 | The maximum number N that varies from 1 to 200. 1196 | 1197 | The output format: 1198 | 1199 | All even numbers less than N in ascending order. Each number must be on a separate line. 1200 | 1201 | Sample Input 1: 1202 | 1203 | 8 1204 | Sample Output 1: 1205 | 1206 | 2 1207 | 4 1208 | 6 1209 | ''' 1210 | a = int(input()) 1211 | counter = 1 1212 | 1213 | while counter < a: 1214 | if counter % 2 == 0: 1215 | print(counter) 1216 | counter += 1 1217 | 1218 | ''' 1219 | Code Challenge — Write a program 1220 | Write a program that calculates the factorial of the input number using while loop. 1221 | 1222 | The input format: 1223 | 1224 | The number N in range from 1 to 100. 1225 | 1226 | The output format: 1227 | 1228 | The factorial N!. 1229 | ''' 1230 | a = int(input()) 1231 | counter = 1 1232 | total = 1 1233 | 1234 | while counter <= a: 1235 | total *= counter 1236 | counter += 1 1237 | print(total) 1238 | 1239 | # Python → Functions → Invoking a function 1240 | 1241 | ''' 1242 | Invoking a function → Young and beautiful 1243 | Code Challenge — Write a program 1244 | Find the youngest person among Jack, Alex, Lana and print this person's age. 1245 | 1246 | Sample Input 1: 1247 | 1248 | 23 1249 | 42 1250 | 34 1251 | Sample Output 1: 1252 | 1253 | 23 1254 | ''' 1255 | jack_age = int(input()) 1256 | alex_age = int(input()) 1257 | lana_age = int(input()) 1258 | 1259 | print(min(jack_age, alex_age, lana_age)) 1260 | 1261 | ''' 1262 | Invoking a function → Hello, world! 1263 | Code Challenge — Write a program 1264 | The classical introductory exercise, slightly modified. Write a program that takes a string, writes it to the variable name (this part of code is already written for you) and then prints "Hello, world! Hello, name". 1265 | 1266 | The variable name is already defined. 1267 | 1268 | Sample Input 1: 1269 | 1270 | John 1271 | Sample Output 1: 1272 | 1273 | Hello, world! Hello, John 1274 | ''' 1275 | name = input() 1276 | 1277 | print("Hello, world! Hello,", name) 1278 | 1279 | ''' 1280 | Invoking a function → String length 1281 | Code Challenge — Write a program 1282 | Write a program that takes a string and prints its length. 1283 | 1284 | The variable string is already defined. 1285 | 1286 | Sample Input 1: 1287 | 1288 | foo 1289 | Sample Output 1: 1290 | 1291 | 3 1292 | ''' 1293 | string = input() 1294 | print(len(string)) 1295 | 1296 | ''' 1297 | Invoking a function → Minimal number 1298 | Code Challenge — Write a program 1299 | Write a program that takes two integer numbers x and y and then prints the minimum of them. 1300 | 1301 | Variables x and y are already defined for you. 1302 | 1303 | Sample Input 1: 1304 | 1305 | 8 1306 | 11 1307 | Sample Output 1: 1308 | 1309 | 8 1310 | Sample Input 2: 1311 | 1312 | 935 1313 | 42 1314 | Sample Output 2: 1315 | 1316 | 42 1317 | ''' 1318 | x = int(input()) 1319 | y = int(input()) 1320 | # the variables `x` and `y` are defined, so just print the minimum 1321 | print(min(x, y)) 1322 | 1323 | ''' 1324 | Invoking a function → Longest word 1325 | Code Challenge — Write a program 1326 | Find the longest word in a pair and print its length. 1327 | 1328 | The variables word1 and word2 are defined for you. 1329 | 1330 | Sample Input 1: 1331 | 1332 | Riddikulus 1333 | Alohomora 1334 | Sample Output 1: 1335 | 1336 | 10 1337 | Sample Input 2: 1338 | 1339 | earthquake 1340 | Supercalifragilisticexpialidocious 1341 | Sample Output 2: 1342 | 1343 | 34 1344 | ''' 1345 | word1 = input() 1346 | word2 = input() 1347 | 1348 | # How many letters does the longest word contain? 1349 | print(max(len(word1), len(word2))) 1350 | 1351 | # Python → Functions → Declaring a function 1352 | 1353 | ''' 1354 | Declaring a function → Define the function 1355 | Code Challenge — Write a program 1356 | Define the function f(x) that returns the value of the following function defined on the complete number scale: 1357 | 1358 | f(x)= \begin{cases} 1 - (x + 2)^2,\quad &\text{if } x\le -2\\ -\frac x2 ,\quad &\text{if } -2 \lt x \le 2\\ (x-2)^2 + 1,\quad &\text{if } x \gt 2 \end{cases} 1359 | f(x)= 1360 | ⎩ 1361 | ⎪ 1362 | ⎪ 1363 | ⎨ 1364 | ⎪ 1365 | ⎪ 1366 | ⎧ 1367 | ​ 1368 | 1369 | 1−(x+2) 1370 | 2 1371 | , 1372 | − 1373 | 2 1374 | x 1375 | ​ 1376 | , 1377 | (x−2) 1378 | 2 1379 | +1, 1380 | ​ 1381 | 1382 | if x≤−2 1383 | if −22 1385 | ​ 1386 | 1387 | 1388 | Just implement the function, handling input/output is NOT required. 1389 | 1390 | Sample Input 1: 1391 | 1392 | 4.5 1393 | Sample Output 1: 1394 | 1395 | 7.25 1396 | Sample Input 2: 1397 | 1398 | -4.5 1399 | Sample Output 2: 1400 | 1401 | -5.25 1402 | Sample Input 3: 1403 | 1404 | 1 1405 | Sample Output 3: 1406 | 1407 | -0.5 1408 | ''' 1409 | def f(x): 1410 | # put your python code here 1411 | if x <= -2: 1412 | return 1 - (x + 2) ** 2 1413 | elif -2 < x and x <= 2: 1414 | return -0.5 * x 1415 | else: 1416 | return (x - 2) ** 2 + 1 1417 | 1418 | ''' 1419 | Declaring a function → Fahrenheit 1420 | Code Challenge — Write a program 1421 | Convert the temperature from Fahrenheit to Celsius in the function below. You can use this formula: 1422 | C∘ =(F∘ −32)× 5/9 1423 | 1424 | Round the returned result to 3 decimal places. 1425 | 1426 | You don't have to handle input, just implement the function below. 1427 | ''' 1428 | def fahrenheit_to_celsius(num): 1429 | return round((num - 32) * (5 / 9), 3) 1430 | 1431 | ''' 1432 | Declaring a function → Make the function work 1433 | Code Challenge — Write a program 1434 | The function closest_mod_5 takes exactly one integer argument x and returns the smallest integer y such that: 1435 | 1436 | y is greater than or equal to x, 1437 | y is divisible by 5. 1438 | Copy this code and edit the last line to make the function work: 1439 | 1440 | def closest_mod_5(x): 1441 | if x % 5 == 0: 1442 | return x 1443 | return "I don't know :(" 1444 | ''' 1445 | def closest_mod_5(x): 1446 | if x % 5 == 0: 1447 | return x 1448 | return x + (5 - x % 5) 1449 | 1450 | ''' 1451 | Declaring a function → The Sum of 2 1452 | Code Challenge — Write a program 1453 | Implement a function get_sum(a, b) that will return the sum of two numbers. 1454 | 1455 | Don't bother about input/output. You just have to implement the function which will return a value. Please, do not rename the function. 1456 | 1457 | Sample Input 1: 1458 | 1459 | 8 11 1460 | Sample Output 1: 1461 | 1462 | 19 1463 | ''' 1464 | # implement the function below 1465 | def get_sum(a, b): 1466 | return a + b 1467 | 1468 | ''' 1469 | Declaring a function → Captain 1470 | Code Challenge — Write a program 1471 | Define a function that will add the word "captain" before the name of a person. 1472 | 1473 | The function should be named captain_adder, take one argument name and print the string, i.e. it doesn't have to return anything. 1474 | 1475 | Sample Input: 1476 | 1477 | Jack Sparrow 1478 | 1479 | Sample Output: 1480 | 1481 | captain Jack Sparrow 1482 | ''' 1483 | # declare your function here 1484 | def captain_adder(name): 1485 | print("captain", name) 1486 | 1487 | ''' 1488 | Declaring a function → Miles away 1489 | Code Challenge — Write a program 1490 | Let's take your skill of converting miles to kilometers to the next level! Define a function that accepts the number of miles and returns this distance in kilometers. 1491 | 1492 | Assume that one mile is approximately equal to 1.609 kilometers. 1493 | 1494 | You don't have to handle input, just implement the function. 1495 | ''' 1496 | def mi_to_km(miles): 1497 | return miles * 1.609 1498 | 1499 | # Python → Control flow statements → Else statement 1500 | 1501 | ''' 1502 | Else statement → Spellchecker 1503 | Code Challenge — Write a program 1504 | Write a simple spellchecker that tells you if the word is spelled correctly. Use the dictionary in the code below! 1505 | 1506 | The input format: 1507 | 1508 | A single line with the "word". 1509 | 1510 | The output format: 1511 | 1512 | If the word in spelled correctly write Correct, otherwise, Incorrect. 1513 | 1514 | Sample Input 1: 1515 | 1516 | aa 1517 | Sample Output 1: 1518 | 1519 | Correct 1520 | ''' 1521 | dictionary = ["aa", "abab", "aac", "ba", "bac", "baba", "cac", "caac"] 1522 | word = input() 1523 | 1524 | if word in dictionary: 1525 | print("Correct") 1526 | else: 1527 | print("Incorrect") 1528 | 1529 | ''' 1530 | Else statement → Minimum and maximum 1531 | Code Challenge — Write a program 1532 | Imagine that your friend asked you to write a program that finds the minimum and the maximum. 1533 | 1534 | Write the code that receives two integers as its input, each number goes on a new line. The output should show: 1535 | 1536 | The biggest number - in the first line 1537 | The smallest number - in the second line. 1538 | Note that your friend might insert identical numbers! Just output both given numbers in this case. 1539 | 1540 | Sample Input 1: 1541 | 1542 | 2 1543 | 8 1544 | Sample Output 1: 1545 | 1546 | 8 1547 | 2 1548 | ''' 1549 | a = int(input()) 1550 | b = int(input()) 1551 | 1552 | max_num = max(a, b) 1553 | min_num = min(a, b) 1554 | 1555 | print(max_num) 1556 | print(min_num) 1557 | 1558 | ''' 1559 | Else statement → Leap Year 1560 | Code Challenge — Write a program 1561 | Write a program that checks if a year is leap. 1562 | 1563 | A year is considered leap if it is divisible by 4 and NOT divisible by 100, or if it is divisible by 400. So, 2000 is leap and 2100 isn't. 1564 | 1565 | The program should work correctly on the numbers 1900≤n≤3000. 1566 | 1567 | Output either "Leap" or "Ordinary" depending on the input. 1568 | 1569 | Sample Input 1: 1570 | 1571 | 2100 1572 | Sample Output 1: 1573 | 1574 | Ordinary 1575 | Sample Input 2: 1576 | 1577 | 2000 1578 | Sample Output 2: 1579 | 1580 | Leap 1581 | ''' 1582 | # put your python code here 1583 | year = int(input()) 1584 | 1585 | if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: 1586 | print("Leap") 1587 | else: 1588 | print("Ordinary") 1589 | 1590 | 1591 | # Python → Data types and operations → Quotes and multi-line strings 1592 | 1593 | ''' 1594 | Quotes and multi-line strings → Taking turns 1595 | 1596 | Write a program that will print the following multi-line string: 1597 | 1598 | ' '' ''' 1599 | ' '' ''' 1600 | ' '' ''' 1601 | ''' 1602 | 1603 | print("' '' '''") 1604 | print("' '' '''") 1605 | print("' '' '''") 1606 | 1607 | ''' 1608 | Quotes and multi-line strings → Poetry 1609 | 1610 | Write a program that will create and print this string from "How The Grinch Stole Christmas": 1611 | 1612 | Did that stop the old Grinch? 1613 | No! The Grinch simply said, 1614 | "If I can't find a reindeer, 1615 | I'll make one instead!" 1616 | ''' 1617 | 1618 | print("Did that stop the old Grinch?") 1619 | print("No! The Grinch simply said,") 1620 | print("\"If I can't find a reindeer,") 1621 | print("I'll make one instead!\"") 1622 | 1623 | ''' 1624 | Quotes and multi-line strings → Possessive case 1625 | 1626 | Make a phrase with a possessive noun out of two strings and print it, for example, the phrase philosopher's stone given the strings philosopher and stone. 1627 | ''' 1628 | 1629 | word1 = "King" 1630 | word2 = "Cross" 1631 | # add the correct string instead of the dots 1632 | print(word1 + "\'s " + word2) 1633 | 1634 | ''' 1635 | Quotes and multi-line strings → Triple-quoted string 1636 | 1637 | Create a program that will print this multi-line string containing triple double quotes: 1638 | 1639 | """ 1640 | THIS IS A STRING 1641 | """ 1642 | ''' 1643 | 1644 | print("\"\"\"") 1645 | print("THIS IS A STRING") 1646 | print("\"\"\"") 1647 | 1648 | ''' 1649 | Quotes and multi-line strings → A pyramid! 1650 | 1651 | Write a program that prints a 4-line string, containing: 1652 | 1653 | at the first line – one single quote 1654 | at the second line – two single quotes with one double quote inside 1655 | at the third line – five alternating single and double quotes, starting with a single quote 1656 | at the fourth line – seven alternating single and double quotes, starting with a single quote 1657 | 1658 | So, the output should look like this: 1659 | 1660 | ' 1661 | '"' 1662 | '"'"' 1663 | '"'"'"' 1664 | ''' 1665 | 1666 | print("'") 1667 | print("'\"'") 1668 | print("\'\"\'\"\'") 1669 | print("\'\"\'\"\'\"\'") 1670 | 1671 | # Python → Collections → List 1672 | 1673 | ''' 1674 | List → An empty list! 1675 | Write a program that will create and print an empty list. 1676 | ''' 1677 | empty_list = [] 1678 | print(empty_list) 1679 | 1680 | ''' 1681 | List → Single-element list 1682 | Helen wants to create a list containing only one element – a string with her name. To do this, she wrote this line of code: 1683 | 1684 | name = list('Helen') 1685 | 1686 | However, when she prints the list, her name appears broken into several one-letter strings: 1687 | 1688 | print(name) 1689 | # ['H', 'e', 'l', 'e', 'n'] 1690 | ''' 1691 | name = ['Helen'] 1692 | print(name) 1693 | 1694 | ''' 1695 | List → Reveal the hidden 1696 | There's a predefined variable hidden that contains a list. Count the number of elements in this list and print it. 1697 | ''' 1698 | # use the variable "hidden", it is already defined 1699 | print(len(hidden)) 1700 | 1701 | ''' 1702 | List → List from string 1703 | Write a program that constructs a list from the given string's symbols and then prints it. 1704 | 1705 | The variable string has been defined for you. 1706 | 1707 | Sample Input 1: 1708 | 1709 | python 1710 | 1711 | Sample Output 1: 1712 | 1713 | ['p', 'y', 't', 'h', 'o', 'n'] 1714 | 1715 | Sample Input 2: 1716 | 1717 | John 1718 | 1719 | Sample Output 2: 1720 | 1721 | ['J', 'o', 'h', 'n'] 1722 | ''' 1723 | # work with this variable 1724 | string = input() 1725 | print(list(string)) 1726 | 1727 | 1728 | # Python → Collections → Indexes 1729 | 1730 | ''' 1731 | Indexes → Tail 1732 | Sentences generally end with a certain punctuation mark: a period ., an exclamation point !, or a question mark ?. 1733 | 1734 | Find out which of these symbols marks the end of a string stored in the variable sentence and print it out. 1735 | 1736 | Sample Input 1: 1737 | 1738 | What a lovely day! 1739 | 1740 | Sample Output 1: 1741 | 1742 | ! 1743 | ''' 1744 | sentence = input() 1745 | # print the last symbol below 1746 | print(sentence[len(sentence) - 1]) 1747 | 1748 | ''' 1749 | Indexes → Initials 1750 | Find the initial letter of a person's name and print it out. 1751 | 1752 | Make use of the variable name that stores a string. 1753 | 1754 | Sample Input 1: 1755 | 1756 | Kate 1757 | 1758 | Sample Output 1: 1759 | 1760 | K 1761 | 1762 | Sample Input 2: 1763 | 1764 | Ivor 1765 | 1766 | Sample Output 2: 1767 | 1768 | I 1769 | ''' 1770 | name = input() 1771 | # print the initial letter 1772 | print(name[0]) 1773 | 1774 | ''' 1775 | Indexes → Find the third number 1776 | The list prices contains several numbers. Print the third number from this list. 1777 | 1778 | You don't have to handle the input. 1779 | 1780 | Sample Input 1: 1781 | 1782 | [5230, 5661, 5081, 9539, 5563] 1783 | 1784 | Sample Output 1: 1785 | 1786 | 5081 1787 | ''' 1788 | # work with the list 'prices' here 1789 | print(prices[2]) 1790 | 1791 | ''' 1792 | Indexes → Latin alphabet 1793 | We have created a variable for the lowercase English alphabet: 1794 | 1795 | alphabet = 'abcdefghijklmnopqrstuvwxyz' 1796 | 1797 | Your task is to print the 15th letter of this string. 1798 | ''' 1799 | alphabet = 'abcdefghijklmnopqrstuvwxyz' 1800 | # put your python code here 1801 | print(alphabet[14]) 1802 | 1803 | ''' 1804 | Indexes → Update a list 1805 | Modify the list numbers so that each number in it coincides with its index (not the negative one). 1806 | 1807 | In the end, print the list. 1808 | ''' 1809 | numbers = [4, 1, 0, 3, 2, 5] 1810 | # put your python code here 1811 | numbers = [0, 1, 2, 3, 4, 5] 1812 | print(numbers) 1813 | 1814 | ''' 1815 | Indexes → Modifying data 1816 | Lists, unlike strings, are mutable. We can use that to modify their data with indexes. 1817 | 1818 | There is a list planets with the names of the Solar system planets. However, instead of the 5th planet, there's an X. Reassign it to the real name of the 5th planet. 1819 | 1820 | Note that the list planets has already been defined, you just need to change one element. 1821 | ''' 1822 | # change the name of the 5th planet in planets 1823 | planets[4] = "Jupiter" 1824 | 1825 | # Python → Control flow statements → Elif statement 1826 | 1827 | ''' 1828 | Elif statement → Menu 1829 | Let's say you were asked to create a program for a restaurant: a visitor enters what kind of food they would like to order and gets back the restaurant's offer. 1830 | 1831 | The restaurant has just opened so its menu contains only a few options: 1832 | 1833 | pizza: Margarita, Four Seasons, Neapoletana, Vegetarian, Spicy 1834 | salad: Caesar salad, Green salad, Tuna salad, Fruit salad 1835 | soup: Chicken soup, Ramen, Tomato soup, Mushroom cream soup 1836 | 1837 | If the visitors asks for something that is not in the menu, the program should write "Sorry, we don't have it in the menu". 1838 | 1839 | Input: "pizza" 1840 | Output: "Margarita, Four Seasons, Neapoletana, Vegetarian, Spicy" 1841 | 1842 | Input: "burger" 1843 | Output: "Sorry, we don't have it in the menu" 1844 | 1845 | Sample Input 1: 1846 | 1847 | pizza 1848 | 1849 | Sample Output 1: 1850 | 1851 | Margarita, Four Seasons, Neapoletana, Vegetarian, Spicy 1852 | 1853 | ''' 1854 | food = input() 1855 | if food == "pizza": 1856 | print("Margarita, Four Seasons, Neapoletana, Vegetarian, Spicy") 1857 | elif food == "salad": 1858 | print("Caesar salad, Green salad, Tuna salad, Fruit salad") 1859 | elif food == "soup": 1860 | print("Chicken soup, Ramen, Tomato soup, Mushroom cream soup") 1861 | else: 1862 | print("Sorry, we don't have it in the menu") 1863 | 1864 | ''' 1865 | Elif statement → What day is it? 1866 | 1867 | Today the whole world uses the Coordinated Universal Time (UTC) to distinguish between the time zones. The UTC is considered to be the 0, and the rest of the time zones are expressed using positive or negative offsets from the UTC. For instance, London is in the zone UTC+00:00 (or GMT) and Moscow is in the zone UTC+3:00. 1868 | 1869 | There are 14 positive offsets (from UTC+1:00 to UTC+14:00) and 12 negative offsets (from UTC-12:00 to UTC-1:00). This also means that at a particular hour, three calendar days are observed on the planet. 1870 | 1871 | Imagine, it is 10:30 on a Tuesday in London (UTC). What day of the week is it in a specific timezone? 1872 | 1873 | The input format: 1874 | 1875 | The value of offset with the sign (e.g. +3 or -9). 1876 | 1877 | The output format: 1878 | 1879 | The day of the week in that timezone. 1880 | 1881 | Sample Input 1: 1882 | 1883 | 0 1884 | 1885 | Sample Output 1: 1886 | 1887 | Tuesday 1888 | 1889 | Sample Input 2: 1890 | 1891 | -11 1892 | 1893 | Sample Output 2: 1894 | 1895 | Monday 1896 | ''' 1897 | offset = input() 1898 | if offset == "0": 1899 | print("Tuesday") 1900 | elif len(offset) == 2: 1901 | print("Tuesday") 1902 | else: 1903 | if offset[0] == "-" and int(str(offset[1]) + str(offset[2])) > 10: 1904 | print("Monday") 1905 | elif offset[0] == "+" and int(str(offset[1]) + str(offset[2])) == 14: 1906 | print("Wednesday") 1907 | else: 1908 | print("Tuesday") 1909 | 1910 | ''' 1911 | Elif statement → Computer hours 1912 | 1913 | Write a program that asks a user how long, on average, they spend on a computer per day and: 1914 | 1915 | if it is less than 2 hours says 'That seems reasonable' 1916 | else if it is less than 4 hours per day says 'Do you have time for anything else?' 1917 | else the programs says 'You need to get outside more!' 1918 | 1919 | Sample Input 1: 1920 | 1921 | 6 1922 | 1923 | Sample Output 1: 1924 | 1925 | You need to get outside more! 1926 | ''' 1927 | # Make sure your output matches the assignment *exactly* 1928 | computer_hours = int(input()) 1929 | if computer_hours < 2: 1930 | print("That seems reasonable") 1931 | elif computer_hours < 4: 1932 | print("Do you have time for anything else?") 1933 | else: 1934 | print("You need to get outside more!") 1935 | 1936 | ''' 1937 | Elif statement → Calculator 1938 | 1939 | Let's write a simple calculator! 1940 | 1941 | It will read 3 lines: 1942 | 1943 | the first number 1944 | the second number 1945 | the arithmetic operation. 1946 | 1947 | Numbers are floats! 1948 | 1949 | The output is the result of the following: first_number operation second_number. 1950 | 1951 | Operations are: +, -, /, *, mod, pow, div. 1952 | mod — modulo operation, i.e. the remainder of the division first_numer % second_number, 1953 | pow — exponentiation, the first number will be the base and the second — the power: first_number ** second_number, 1954 | div — integer division first_number // second_number. 1955 | 1956 | Note that if the second number is 0 and you want to perform any of the operations /, mod, or div, the calculator should say "Division by 0!" 1957 | 1958 | Sample Input 1: 1959 | 1960 | 5.0 1961 | 0.0 1962 | mod 1963 | 1964 | Sample Output 1: 1965 | 1966 | Division by 0! 1967 | 1968 | Sample Input 2: 1969 | 1970 | -12.0 1971 | -8.0 1972 | * 1973 | 1974 | Sample Output 2: 1975 | 1976 | 96.0 1977 | 1978 | Sample Input 3: 1979 | 1980 | 5.0 1981 | 10.0 1982 | / 1983 | 1984 | Sample Output 3: 1985 | 1986 | 0.5 1987 | ''' 1988 | # put your python code here 1989 | n1 = float(input()) 1990 | n2 = float(input()) 1991 | op = input() 1992 | # +, -, /, *, mod, pow, div. 1993 | if op == "+": 1994 | print(n1 + n2) 1995 | elif op == "-": 1996 | print(n1 - n2) 1997 | elif op == "/": 1998 | if int(n2) == 0: 1999 | print("Division by 0!") 2000 | else: 2001 | print(n1 / n2) 2002 | elif op == "*": 2003 | print(n1 * n2) 2004 | elif op == "mod": 2005 | if int(n2) == 0: 2006 | print("Division by 0!") 2007 | else: 2008 | print(n1 % n2) 2009 | elif op == "pow": 2010 | print(n1 ** n2) 2011 | else: 2012 | if int(n2) == 0: 2013 | print("Division by 0!") 2014 | else: 2015 | print(n1 // n2) 2016 | 2017 | ''' 2018 | Elif statement → Particles 2019 | The world of elementary particles is rather complex. There are many different classes and they can interact in a rather interesting way. 2020 | 2021 | Two important characteristics of the elementary particles are the spin and the electric charge. Here are some of the elementary particles: 2022 | Particle Class Spin Electric charge 2023 | Strange Quark 1/2 -1/3 2024 | Charm Quark 1/2 2/3 2025 | Electron Lepton 1/2 -1 2026 | Muon Lepton 1/2 0 2027 | Photon Boson 1 0 2028 | Higgs boson Boson 0 0 2029 | 2030 | Write a program that returns the particle and its class based on its spin and electric charge. 2031 | 2032 | The input format: 2033 | 2034 | Two lines: first the spin of the particle, then its charge. 2035 | 2036 | The output format: 2037 | 2038 | The particle and its class separated by a space. 2039 | 2040 | Sample Input 1: 2041 | 2042 | 1/2 2043 | -1/3 2044 | 2045 | Sample Output 1: 2046 | 2047 | Strange Quark 2048 | ''' 2049 | spin = input() 2050 | charge = input() 2051 | if spin == "1/2": 2052 | if charge == "-1/3": 2053 | print("Strange Quark") 2054 | elif charge == "2/3": 2055 | print("Charm Quark") 2056 | elif charge == "-1": 2057 | print("Electron Lepton") 2058 | else: 2059 | print("Muon Lepton") 2060 | elif spin == "1": 2061 | print("Photon Boson") 2062 | else: 2063 | print("Higgs boson Boson") 2064 | 2065 | 2066 | ''' 2067 | Elif statement → Grade 2068 | There is a number of grades you can get in a test: A, B, C, D, F. The percentages are the following: 2069 | 2070 | A: 90-100% 2071 | 2072 | B: 80-90% 2073 | 2074 | C: 70-80% 2075 | 2076 | D: 60-70% 2077 | 2078 | F: <60% 2079 | 2080 | Determine the grade that a student will get based on the student's score and the maximum score. 2081 | 2082 | Note that the upper limit is not included in the range, except for the A grade. For example, a student with 60% will get D, with 70% or 79.9% — C, but the top score 100% is just A. 2083 | 2084 | The input format: 2085 | 2086 | Two lines: the first with a student's score and the second with the maximum. 2087 | 2088 | The output format: 2089 | 2090 | The grade of the student. 2091 | 2092 | Sample Input 1: 2093 | 2094 | 75 2095 | 100 2096 | 2097 | Sample Output 1: 2098 | 2099 | C 2100 | 2101 | Sample Input 2: 2102 | 2103 | 100 2104 | 200 2105 | 2106 | Sample Output 2: 2107 | 2108 | F 2109 | ''' 2110 | student_score = int(input()) 2111 | max_score = int(input()) 2112 | 2113 | if (student_score / max_score) * 100 >= 90: 2114 | print("A") 2115 | elif (student_score / max_score) * 100 >= 80 and (student_score / max_score) * 100 < 90: 2116 | print("B") 2117 | elif (student_score / max_score) * 100 >= 70 and (student_score / max_score) * 100 < 80: 2118 | print("C") 2119 | elif (student_score / max_score) * 100 >= 60 and (student_score / max_score) * 100 < 70: 2120 | print("D") 2121 | else: 2122 | print("F") 2123 | 2124 | ''' 2125 | Elif statement → Index of synthesis 2126 | 2127 | One way to classify the languages of the world is by looking at their morphological systems. One classification is based on the index of synthesis that reflects an average number of morphemes in a word. The values vary between 1 and 4 and there are 3 types of languages according to that system. Here are they: 2128 | 2129 | Type — Index 2130 | 2131 | Analytic — less than 2 2132 | 2133 | Synthetic — from 2 to 3 2134 | 2135 | Polysynthetic — more than 3 2136 | 2137 | Write a program that given the index of synthesis determines the type of the language. 2138 | 2139 | The input format: 2140 | 2141 | The value of the index of synthesis. 2142 | 2143 | The output format: 2144 | 2145 | The type of language. 2146 | 2147 | Sample Input 1: 2148 | 2149 | 2.35 2150 | 2151 | Sample Output 1: 2152 | 2153 | Synthetic 2154 | 2155 | Sample Input 2: 2156 | 2157 | 1.68 2158 | 2159 | Sample Output 2: 2160 | 2161 | Analytic 2162 | ''' 2163 | value = float(input()) 2164 | if value < 2: 2165 | print("Analytic") 2166 | elif 2 <= value <= 3: 2167 | print("Synthetic") 2168 | else: 2169 | print("Polysynthetic") 2170 | 2171 | ''' 2172 | Elif statement → The farm 2173 | 2174 | In a farming game, you can buy certain animals for a specific price. As a player, you want to buy the most useful (i.e. the most expensive) animal. Here are the animals and their prices: 2175 | 2176 | Item: Price 2177 | 2178 | Chicken: 23 2179 | 2180 | Goat: 678 2181 | 2182 | Pig: 1296 2183 | 2184 | Cow: 3848 2185 | 2186 | Sheep: 6769 2187 | 2188 | Write a program that determines what is the most expensive animal that the user can buy with their money and how many of them they can buy. 2189 | 2190 | The input format: 2191 | 2192 | The money that the user has. 2193 | 2194 | The output format: 2195 | 2196 | How many animals the user can afford, for example, 20 chickens or 4 cows. If the user cannot afford any animal, write None. 2197 | 2198 | Pay attention to the number of nouns. Also, keep in mind that the word "sheep" has the irregular plural form "sheep". 2199 | 2200 | Sample Input 1: 2201 | 2202 | 25 2203 | 2204 | Sample Output 1: 2205 | 2206 | 1 chicken 2207 | 2208 | Sample Input 2: 2209 | 2210 | 8 2211 | 2212 | Sample Output 2: 2213 | 2214 | None 2215 | 2216 | Sample Input 3: 2217 | 2218 | 668 2219 | 2220 | Sample Output 3: 2221 | 2222 | 29 chickens 2223 | ''' 2224 | money = int(input()) 2225 | count = 0 2226 | if money < 23: 2227 | print("None") 2228 | elif 23 <= money < 678: 2229 | count = money // 23 2230 | if count == 1: 2231 | print("1 chicken") 2232 | else: 2233 | print(str(count) + " chickens") 2234 | elif 678 <= money < 1296: 2235 | count = money // 678 2236 | if count == 1: 2237 | print("1 goat") 2238 | else: 2239 | print(str(count) + " goats") 2240 | elif 1296 <= money < 3848: 2241 | count = money // 1296 2242 | if count == 1: 2243 | print("1 pig") 2244 | else: 2245 | print(str(count) + " pigs") 2246 | elif 3848 <= money < 6769: 2247 | count = money // 3848 2248 | if count == 1: 2249 | print("1 cow") 2250 | else: 2251 | print(str(count) + " cows") 2252 | else: 2253 | count = money // 6769 2254 | print(str(count) + " sheep") 2255 | 2256 | ''' 2257 | Elif statement → The army of units 2258 | In a computer game, each gamer has an army of units. 2259 | 2260 | Write a program that will classify the army of your enemies corresponding to the following rules: 2261 | 2262 | 2263 | 2264 | Units: Category 2265 | 2266 | less than 1: no army 2267 | 2268 | from 1 to 9: few 2269 | 2270 | from 10 to 49: pack 2271 | 2272 | from 50 to 499: horde 2273 | 2274 | from 500 to 999: swarm 2275 | 2276 | 1000 and more: legion 2277 | 2278 | 2279 | 2280 | The program should read the number of units and output the corresponding category. 2281 | 2282 | Sample Input 1: 2283 | 2284 | 18 2285 | 2286 | Sample Output 1: 2287 | 2288 | pack 2289 | 2290 | Sample Input 2: 2291 | 2292 | 5 2293 | 2294 | Sample Output 2: 2295 | 2296 | few 2297 | ''' 2298 | units = int(input()) 2299 | if units < 1: 2300 | print("no army") 2301 | elif 1 <= units <= 9: 2302 | print("few") 2303 | elif 10 <= units <= 49: 2304 | print("pack") 2305 | elif 50 <= units <= 499: 2306 | print("horde") 2307 | elif 500 <= units <= 999: 2308 | print("swarm") 2309 | else: 2310 | print("legion") 2311 | 2312 | ''' 2313 | Elif statement → Healthy sleep 2314 | 2315 | Ann watched a TV program about health and learned that it is recommended to sleep at least AA hours per day, but oversleeping is also not healthy and you should not sleep more than BB hours. Now, Ann sleeps HH hours per day. 2316 | 2317 | If Ann's sleep schedule complies with the requirements of that TV program, print "Normal". If Ann sleeps less than AA hours, output "Deficiency", if she sleeps more than BB hours, output "Excess". 2318 | 2319 | The input comprises three strings with variables in the following order: A A A, B B B, H H H. 2320 | 2321 | A A A is always less than or equal to B B B. 2322 | 2323 | Sample Input 1: 2324 | 2325 | 6 2326 | 10 2327 | 8 2328 | 2329 | Sample Output 1: 2330 | 2331 | Normal 2332 | 2333 | Sample Input 2: 2334 | 2335 | 7 2336 | 9 2337 | 10 2338 | 2339 | Sample Output 2: 2340 | 2341 | Excess 2342 | 2343 | Sample Input 3: 2344 | 2345 | 7 2346 | 9 2347 | 2 2348 | 2349 | Sample Output 3: 2350 | 2351 | Deficiency 2352 | ''' 2353 | # put your python code here 2354 | 2355 | input_a = int(input()) 2356 | input_b = int(input()) 2357 | input_h = int(input()) 2358 | 2359 | if input_h < input_a: 2360 | print("Deficiency") 2361 | elif input_h > input_b: 2362 | print("Excess") 2363 | else: 2364 | print("Normal") 2365 | 2366 | ''' 2367 | Elif statement → Positive, Negative or Zero 2368 | 2369 | Write a program that reads an integer number and prints: 2370 | 2371 | "negative" if the number is less than 0; 2372 | "positive" if the number is greater than 0; 2373 | "zero" if the number equals 0. 2374 | 2375 | Do not output double quotes. 2376 | 2377 | Sample Input 1: 2378 | 2379 | -5 2380 | 2381 | Sample Output 1: 2382 | 2383 | negative 2384 | 2385 | ''' 2386 | # put your python code here 2387 | num = int(input()) 2388 | if num < 0: 2389 | print("negative") 2390 | elif num > 0: 2391 | print("positive") 2392 | else: 2393 | print("zero") 2394 | 2395 | ''' 2396 | Elif statement → Recommender system 2397 | 2398 | Write a program that recommends one of the following movies based on the age of a user: 2399 | 2400 | <=16<= 16<=16 "Lion King" 2401 | 2402 | 17−2517 - 2517−25 "Trainspotting" 2403 | 2404 | 26−4026 - 4026−40 "Matrix" 2405 | 2406 | 41−6041 - 6041−60 "Pulp Fiction" 2407 | 2408 | >60> 60>60 "Breakfast at Tiffany's" 2409 | 2410 | The user enters their age and the program outputs one title. 2411 | 2412 | Sample Input 1: 2413 | 2414 | 19 2415 | 2416 | Sample Output 1: 2417 | 2418 | Trainspotting 2419 | 2420 | Sample Input 2: 2421 | 2422 | 71 2423 | 2424 | Sample Output 2: 2425 | 2426 | Breakfast at Tiffany's 2427 | ''' 2428 | age = int(input()) 2429 | if age <= 16: 2430 | print("Lion King") 2431 | elif 17 <= age <= 25: 2432 | print("Trainspotting") 2433 | elif 26 <= age <= 40: 2434 | print("Matrix") 2435 | elif 41 <= age <= 60: 2436 | print("Pulp Fiction") 2437 | else: 2438 | print("Breakfast at Tiffany's") 2439 | 2440 | ''' 2441 | Locate a point on the Cartesian coordinate plane: which of four quadrants does a point belong to? 2442 | 2443 | Read two numbers from the input, not necessarily integers, the first number will indicate a position on the X-axis, the second one — on the Y-axis. Let's keep referring to the quadrants by Roman numerals, as shown in the picture. 2444 | 2445 | The points lying on the axes, with either x = 0 or y = 0, and the origin (0, 0) will not appear in the tests. 2446 | 2447 | Sample Input 1: 2448 | 2449 | 3.987 2450 | -10 2451 | 2452 | Sample Output 1: 2453 | 2454 | IV 2455 | ''' 2456 | 2457 | x = float(input()) 2458 | y = float(input()) 2459 | 2460 | if x > 0 and y > 0: 2461 | print("I") 2462 | elif x < 0 and y > 0: 2463 | print("II") 2464 | elif x < 0 and y < 0: 2465 | print("III") 2466 | else: 2467 | print("IV") 2468 | 2469 | ''' 2470 | Elif statement → Long live the king 2471 | 2472 | Figure out how many moves a chess king can make from a square with given coordinates. The coordinates are numbers between 1 and 8 inclusively. The first number indicates a column, the second one indicates a row. 2473 | 2474 | The king moves one square in any direction (horizontally, vertically, or diagonally). Other rules, as well as special moves, are not taken into account. 2475 | 2476 | Sample Input 1: 2477 | 2478 | 3 2479 | 2 2480 | 2481 | Sample Output 1: 2482 | 2483 | 8 2484 | ''' 2485 | column = int(input()) 2486 | row = int(input()) 2487 | 2488 | if column == 1: 2489 | if row in (1, 8): 2490 | print(3) 2491 | else: 2492 | print(5) 2493 | elif column == 8: 2494 | if row in (1, 8): 2495 | print(3) 2496 | else: 2497 | print(5) 2498 | elif 1 < column < 8: 2499 | if row in (1, 8): 2500 | print(5) 2501 | else: 2502 | print(8) 2503 | else: 2504 | print(8) 2505 | 2506 | # Python → Collections → Tuple 2507 | 2508 | ''' 2509 | Tuple → The last number 2510 | 2511 | Given a tuple consisting of numbers, print its last element. 2512 | 2513 | Sample Input 1: 2514 | 2515 | 24 22 42 2516 | 2517 | Sample Output 1: 2518 | 2519 | 42 2520 | ''' 2521 | # use this tuple 2522 | numbers = tuple(int(n) for n in input().split()) 2523 | print(numbers[len(numbers) - 1]) 2524 | 2525 | ''' 2526 | Tuple → Oceans 2527 | 2528 | Turn a list oceans into a tuple to prevent further data change. 2529 | 2530 | You are NOT supposed to create a new variable for the tuple or print anything. 2531 | ''' 2532 | 2533 | 2534 | 2535 | ''' 2536 | Tuple → Alphabet 2537 | 2538 | Suppose we have a string with some alphabet and we want to store all the letters from it in a tuple. Read the input string and print this tuple. 2539 | 2540 | Sample Input 1: 2541 | 2542 | abcdefghijklmnopqrstuvwxyz 2543 | 2544 | Sample Output 1: 2545 | 2546 | ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') 2547 | ''' 2548 | # work with this string 2549 | alphabet = input() 2550 | print(tuple(alphabet)) 2551 | 2552 | ''' 2553 | Tuple → A single-element tuple 2554 | 2555 | Alice tried to define a singleton tuple with the first and only item [0, 1, 1, 2, 3, 5, 8, 13, 21]. Alas, one small detail is missing here. Fix the code Alice wrote. 2556 | 2557 | You are NOT supposed to create a new variable for the tuple or print anything. 2558 | ''' 2559 | # fix the code below 2560 | singleton = ([0, 1, 1, 2, 3, 5, 8, 13, 21],) 2561 | 2562 | ''' 2563 | Tuple → Name 2564 | 2565 | From the input, you get the first and the last name of a person. Create a tuple with the full name: the first element should be the first name and the second element should be the last name. Save the resulting tuple to the variable full_name. Do NOT print anything! 2566 | 2567 | The variables first_name and last_name have already been read from input. 2568 | 2569 | Example. Suppose, first_name is "John" and last_name is "Doe". The resulting tuple should be ("John", "Doe") 2570 | ''' 2571 | first_name = input() 2572 | last_name = input() 2573 | 2574 | # create full_name here 2575 | full_name = (first_name, last_name) 2576 | 2577 | # Python → Functions → Arguments 2578 | 2579 | ''' 2580 | Arguments → URL 2581 | 2582 | Declare a function called create_url(). It should take two arguments host and port and return URL in the format "https://{host}:{port}". 2583 | 2584 | Set default values for both parameters: a string "localhost" and an integer 443 respectively. Called without arguments, the function would return "https://localhost:443". 2585 | ''' 2586 | def create_url(host="localhost", port=443): 2587 | return "https://" + str(host) + ":" + str(port) 2588 | 2589 | ''' 2590 | Arguments → Favorable default 2591 | 2592 | Write a short function code that will print out this message: 2593 | 2594 | We code in {language} 2595 | 2596 | Set "Python" as a default value to the language parameter. 2597 | ''' 2598 | def code(language="Python"): 2599 | print("We code in", language) 2600 | 2601 | ''' 2602 | Arguments → Call me by my name 2603 | 2604 | There is a function called add_book() that takes a non-empty string containing the title of a book to add it to recommendations. Share your favorite book with us by calling this function. Importantly, specify the keyword argument title in the call. Otherwise, you may get an error. 2605 | ''' 2606 | add_book(title="Stuff") 2607 | 2608 | ''' 2609 | Arguments → Misfortune 2610 | 2611 | Robin has declared a function that prints the squares of odd numbers in the specified range. Unfortunately, several function calls gave an unexpected result. The function itself seems to be fine. Help Robin figure out what went wrong in these calls and fix them. 2612 | 2613 | For each case, the required range is written in a comment. 2614 | ''' 2615 | def square_odds(a, b): 2616 | start = a 2617 | if a % 2 == 0: 2618 | start += 1 2619 | end = b + 1 2620 | for odd in range(start, end, 2): 2621 | print(odd ** 2) 2622 | 2623 | 2624 | # from 22 to 42 2625 | square_odds(22, 42) 2626 | 2627 | # from 15 to 31 2628 | square_odds(15, 31) 2629 | 2630 | # from 42 to 49 2631 | square_odds(42, 49) 2632 | 2633 | 2634 | ''' 2635 | Arguments → Warm welcome 2636 | 2637 | Morgan founded a company that sends satellites into the orbit to observe Earth from space. To headhunt the best aerospace engineers, Morgan decided to pay a welcome bonus equal to 35% of the base salary. However, the bonus may be even higher for a particular candidate. 2638 | 2639 | Declare a function get_bonus() below that would calculate this bonus. It should take two arguments, salary and percentage, with a default value 35 for the last one. Return only the integer part of the bonus. 2640 | ''' 2641 | def get_bonus(salary, percentage=35): 2642 | bonus = (salary * percentage) / 100 2643 | return int(bonus) 2644 | 2645 | # Python → Collections → Operations with list 2646 | 2647 | ''' 2648 | Write a program that takes n numbers as input, creates a list from them and then prints it. 2649 | 2650 | The first input line is n – length of the resulting list. It is followed by n lines containing list elements. 2651 | 2652 | Sample Input 1: 2653 | 2654 | 5 2655 | 100 2656 | -1 2657 | 72 2658 | 0 2659 | 0 2660 | 2661 | Sample Output 1: 2662 | 2663 | [100, -1, 72, 0, 0] 2664 | ''' 2665 | n = int(input()) 2666 | input_list = [] 2667 | for i in range(n): 2668 | input_list.append(int(input())) 2669 | print(input_list) 2670 | 2671 | ''' 2672 | Operations with list → Changed beyond recognition 2673 | Dany created this list: 2674 | 2675 | dragons = ['Rudror', 'Targiss', 'Coporth'] 2676 | 2677 | and then played with it for some time. She used some operations on it, so the result looks like this: 2678 | 2679 | ['Coporth', -10, 'Rudror', 'Targiss'] 2680 | 2681 | Now Dany is confused and wants to understand what she has done exactly. Help her out! Place the operations in the correct order (the order in which they were used, from the first to the last). 2682 | 2683 | Let’s find out what was the last operation Dany did. Sorting can’t be the last operation, since a list containing both numbers and strings can’t be sorted, and inserting -10 to the position 2 can’t be the last operation, because in the resulting list -10 is at the position 1, not the position 2. So, dragons.reverse() was the last operation. From the remaining two, obviously, dragons.sort(reverse=True) was earlier, since you can’t sort a list of strings after inserting an integer number to it. Here are all changes made into a list: 2684 | 2685 | ['Rudror', 'Targiss', 'Coporth'] 2686 | dragons.sort(reverse=True) 2687 | ['Targiss', 'Rudror', 'Coporth'] 2688 | dragons.insert(2, -10) 2689 | ['Targiss', 'Rudror', -10, 'Coporth'] 2690 | dragons.reverse() 2691 | ['Coporth', -10, 'Rudror', 'Targiss'] 2692 | ''' 2693 | 2694 | ''' 2695 | Operations with list → Shopping list 2696 | 2697 | Ruth goes shopping today, so she is busy making a shopping list. 2698 | 2699 | She wrote down things that she would like to buy at the grocery store: first "milk", then "olive oil" and "bananas". When sneaking a look at the fridge, she noticed that there was some milk left and changed her mind about buying a new one. It got crossed out of the list. And, like a cherry on top, a "brownie" became the last item Ruth had put on the list. 2700 | 2701 | Now you try to replicate these operations on the variable shopping_list. 2702 | 2703 | You are NOT supposed to print the results. 2704 | ''' 2705 | # work with this variable 2706 | shopping_list = [] 2707 | 2708 | shopping_list.append("milk") 2709 | # put the rest of list operations below 2710 | shopping_list.append("olive oil") 2711 | shopping_list.append("bananas") 2712 | shopping_list.remove("milk") 2713 | shopping_list.append("brownie") 2714 | 2715 | ''' 2716 | Operations with list → Sort the numbers 2717 | 2718 | Below you can see a list of strings called numbers. Sort it in the descending order as strings (alphabetically) and print the resulting list. 2719 | ''' 2720 | numbers = ["77", "145", "987", "2095", "6", "371", "4999", "81"] 2721 | 2722 | # sort numbers 2723 | numbers.sort(reverse=True) 2724 | print(numbers) 2725 | 2726 | # Python → Modules and packages → Load module 2727 | 2728 | ''' 2729 | Load module → Capitalize all words 2730 | 2731 | Write a program that takes a string, capitalizes all words in it and then prints the result. Use capwords function from the string module. 2732 | 2733 | The string is already defined. 2734 | 2735 | Sample Input 1: 2736 | 2737 | a aaaa aaaaaaaa 2738 | 2739 | Sample Output 1: 2740 | 2741 | A Aaaa Aaaaaaaa 2742 | ''' 2743 | # place `import` statement at top of the program 2744 | import string 2745 | 2746 | # don't modify this code or the variable may not be available 2747 | input_string = input() 2748 | 2749 | # use capwords() here 2750 | print(string.capwords(input_string)) 2751 | 2752 | ''' 2753 | # Load module → Calculating the factorial 2754 | 2755 | Import factorial function from math module and print the value of the factorial of x. 2756 | 2757 | Sample Input 1: 2758 | 2759 | 5 2760 | 2761 | Sample Output 1: 2762 | 2763 | 120 2764 | ''' 2765 | # place `import` statement at top of the program 2766 | from math import factorial 2767 | 2768 | # don't modify this code or variable `x` may not be available 2769 | x = int(input()) 2770 | 2771 | # use factorial() here 2772 | print(factorial(x)) 2773 | 2774 | ''' 2775 | Load module → E ** x minus one 2776 | 2777 | Write a program that takes an integer number x and prints e (a mathematical constant) raised to power x minus one. Use the function expm1 defined in the math module. 2778 | 2779 | The variable x is already defined. 2780 | 2781 | Sample Input 1: 2782 | 2783 | 91 2784 | 2785 | Sample Output 1: 2786 | 2787 | 3.317400098335743e+39 2788 | ''' 2789 | # place `import` statement at top of the program 2790 | from math import expm1 2791 | 2792 | # don't modify this code or variable `x` may not be available 2793 | x = int(input()) 2794 | 2795 | # use expm1() here 2796 | print(expm1(x)) 2797 | 2798 | ''' 2799 | Load module → Working with strings 2800 | 2801 | Print the values of digits and ascii_lowercase from string module on separate lines. 2802 | ''' 2803 | # put your code here 2804 | import string 2805 | 2806 | print(string.digits) 2807 | print(string.ascii_lowercase) 2808 | 2809 | ''' 2810 | Load module → Not exactly random 2811 | 2812 | Numbers, generated by functions provided in random module, are not exactly random – they use random number generator based on some number (by default, on the current system time). You can also set this number with the seed function – and all numerical sequences generated after that will be the same. 2813 | 2814 | Write a program that takes an integer number n, sets a random number generator to that number and then prints a pseudo-random number from -100 to 100. 2815 | 2816 | Use seed and randint functions. The former takes one argument and the latter takes two numbers that represent the range. 2817 | 2818 | The variable n is already defined. 2819 | 2820 | Sample Input 1: 2821 | 2822 | 36 2823 | 2824 | Sample Output 1: 2825 | 2826 | -16 2827 | ''' 2828 | # place `import` statement at top of the program 2829 | import random 2830 | 2831 | # don't modify this code or variable `n` may not be available 2832 | n = int(input()) 2833 | 2834 | # put your code here 2835 | random.seed(n) 2836 | print(random.randint(-100, 100)) 2837 | 2838 | ''' 2839 | Load module → Copysign function 2840 | 2841 | Write a program that takes two float numbers, x and y, and prints x with the sign of y. Use copysign function defined in the math module. 2842 | 2843 | Variables x and y are already defined. 2844 | 2845 | Sample Input 1: 2846 | 2847 | -68.83573394536573 -66.80342071491599 2848 | 2849 | Sample Output 1: 2850 | 2851 | -68.83573394536573 2852 | ''' 2853 | # place `import` statement at top of the program 2854 | from math import copysign 2855 | 2856 | # don't modify this code or the variables may not be available 2857 | x, y = map(float, input().split(' ')) 2858 | 2859 | print(copysign(x, y)) 2860 | 2861 | # Python → Math → Random module 2862 | 2863 | ''' 2864 | Random module → The dice game 2865 | 2866 | It's impossible not to know the dice game. Please, write a program that will imitate a roll of one dice with the help of an appropriate random function. 2867 | 2868 | Thus, your task is just to generate one integer and print it. 2869 | 2870 | The module random has been imported for you. 2871 | ''' 2872 | # The `random` module has been imported. 2873 | # Use a function from it in the next line 2874 | print(random.randint(1, 6)) 2875 | 2876 | ''' 2877 | Random module → From 0 to 1 2878 | 2879 | The function used to set a starting point of the random search algorithm is called random.seed() and takes a single integer number. Write a program that takes an integer number n, sets a seed to n and then prints a pseudo-random number from 0 to 1. 2880 | 2881 | The variable n is already defined. 2882 | 2883 | Sample Input 1: 2884 | 2885 | -38 2886 | 2887 | Sample Output 1: 2888 | 2889 | 0.6394736947203203 2890 | ''' 2891 | import random 2892 | # work with this variable 2893 | n = int(input()) 2894 | random.seed(n) 2895 | print(random.uniform(0, 1)) 2896 | 2897 | ''' 2898 | Random module → Yoda style 2899 | 2900 | Everybody wants to speak like master Yoda sometimes. Let's try to implement a program that will help us with it. 2901 | 2902 | First, we turn the string of words into a list using the string.split() method. 2903 | 2904 | Then use the function random.seed(43) and rearrange the obtained sequence. 2905 | 2906 | To turn the list back into a string, use ' '.join(list). 2907 | 2908 | Print the message in the end. 2909 | 2910 | Note: you have to use random.seed(43) in this task! 2911 | 2912 | Sample Input 1: 2913 | 2914 | Luke, I'm your father 2915 | 2916 | Sample Output 1: 2917 | 2918 | your father I'm Luke, 2919 | 2920 | Sample Input 2: 2921 | 2922 | I will be back 2923 | 2924 | Sample Output 2: 2925 | 2926 | be back will I 2927 | ''' 2928 | import random 2929 | 2930 | # your sentence is assigned to the variable 2931 | sentence = input().split() 2932 | 2933 | # write your python code below 2934 | random.seed(43) 2935 | random.shuffle(sentence) 2936 | 2937 | # shows the message 2938 | print(' '.join(sentence)) 2939 | 2940 | 2941 | ''' 2942 | Random module → Voldemort 2943 | 2944 | The function used to set a starting point of the random search algorithm is called random.seed() and takes a single integer number. Write a program that takes a single number n, sets a seed to n and then prints a pseudo-randomly chosen letter from the string "Voldemort". 2945 | 2946 | The variable n is already defined. 2947 | ''' 2948 | import random 2949 | # work with this variable 2950 | n = int(input()) 2951 | random.seed(n) 2952 | print(random.choice("Voldemort")) 2953 | 2954 | ''' 2955 | Random module → Beta distribution 2956 | 2957 | Python is widely used for mathematical purposes. Calculate the beta distribution where alpha=0.9 and beta=0.1. 2958 | 2959 | Print your results. 2960 | ''' 2961 | import random 2962 | random.seed(3) 2963 | # call the function here 2964 | print(random.betavariate(alpha=0.9, beta=0.1)) 2965 | 2966 | # Python → Data types and operations → String formatting 2967 | 2968 | ''' 2969 | String formatting → Beautify both output and code 2970 | 2971 | The output should be user-friendly, but the code part is also important. Well-structured and readable code is very important for being a good programmer. Now it's up to you to decide, which formatting method to choose. 2972 | 2973 | Imagine you need to compose a dynamic URL for every certain user with user-specific details. Suppose, you want to send different URLs for every user, depending on their name and profession. The base would be something like 2974 | 2975 | "http://example.com/*nickname*/desirable/*profession*/profile", where nickname and profession are prompts from a user and are dynamic. 2976 | 2977 | Prompt a user for their nickname and profession and print a user-specific URL. Don't bother about any rules of composing the URLs and just use raw input to accomplish the task. 2978 | 2979 | Sample Input 1: 2980 | 2981 | raybeam 2982 | cereal-killer 2983 | 2984 | Sample Output 1: 2985 | 2986 | http://example.com/raybeam/desirable/cereal-killer/profile 2987 | 2988 | Sample Input 2: 2989 | 2990 | AnnMelon 2991 | bodybuilder 2992 | 2993 | Sample Output 2: 2994 | 2995 | http://example.com/AnnMelon/desirable/bodybuilder/profile 2996 | ''' 2997 | nickname = input() 2998 | profession = input() 2999 | 3000 | print("http://example.com/{0}/desirable/{1}/profile".format(nickname, profession)) 3001 | 3002 | ''' 3003 | String formatting → How long is that word? 3004 | 3005 | Write a program that calculates the length of the word from the input and prints it out together with the word in the format word has N letters. 3006 | 3007 | Sample Input 1: 3008 | 3009 | serendipity 3010 | 3011 | Sample Output 1: 3012 | 3013 | serendipity has 11 letters 3014 | ''' 3015 | word = input() 3016 | print("{0} has {1} letters".format(word, len(word))) 3017 | 3018 | ''' 3019 | String formatting → Decimal places 3020 | 3021 | Round the number from input to the required number of decimals. 3022 | 3023 | The input format: 3024 | 3025 | Two lines: the first with a floating-point number, the second with an integer representing the decimal count. 3026 | 3027 | The output format: 3028 | 3029 | The rounded number. 3030 | 3031 | Do NOT forget to convert the input numbers and to enclose each value in {} in a formatted string. 3032 | 3033 | Sample Input 1: 3034 | 3035 | 2.71828 3036 | 3 3037 | 3038 | Sample Output 1: 3039 | 3040 | 2.718 3041 | ''' 3042 | num = float(input()) 3043 | place = int(input()) 3044 | 3045 | print(f'{num:.{place}f}') 3046 | 3047 | ''' 3048 | String formatting → Round pi 3049 | 3050 | Write a code that rounds the number pi to 5 decimal places and prints it out. 3051 | 3052 | We've defined the variable pi for you. 3053 | ''' 3054 | pi = 3.141592653589793 3055 | print(f'{pi:.5f}') 3056 | 3057 | ''' 3058 | String formatting → Film 3059 | 3060 | Write a program that gets information about a movie from the input and presents in the following format: 3061 | 3062 | movie (dir. director) came out in year 3063 | 3064 | The input format: 3065 | 3066 | 3 lines: first the title of the movie, then the name of the director and then the year of its release. 3067 | 3068 | Sample Input 1: 3069 | 3070 | Fight Club 3071 | David Fincher 3072 | 1999 3073 | 3074 | Sample Output 1: 3075 | 3076 | Fight Club (dir. David Fincher) came out in 1999 3077 | ''' 3078 | movie = input() 3079 | director = input() 3080 | year = input() 3081 | print(f"{movie} (dir. {director}) came out in {year}".format(movie, director, year)) 3082 | 3083 | ''' 3084 | String formatting → Tax brackets 3085 | 3086 | n progressive tax systems, tax rates change according to the income. Tax brackets are divisions that regulate those changes. 3087 | 3088 | Here's an example of tax brackets in a certain tax system: 3089 | 3090 | 0 — 15,527: 0% tax 3091 | 3092 | 15,528 — 42,707: 15% tax 3093 | 3094 | 42,708 — 85,414: 22% tax 3095 | 3096 | 85,415 — 132,406: 26% tax 3097 | 3098 | 132,407 and more: 28% tax 3099 | 3100 | Suppose we use a simplified version of taxation and apply one tax rate to the entire amount of money. 3101 | 3102 | Write a program that calculates the tax that a person's going to pay based on their income. 3103 | 3104 | The input format: 3105 | 3106 | The value of someone's taxable income (in dollars). 3107 | 3108 | The output format: 3109 | 3110 | The tax for {income} is {percent}%. That is {calculated_tax} dollars! 3111 | 3112 | The calculated_tax should always have 2 decimal places. 3113 | 3114 | Sample Input 1: 3115 | 3116 | 14378 3117 | 3118 | Sample Output 1: 3119 | 3120 | The tax for 14378 is 0%. That is 0.00 dollars! 3121 | 3122 | Sample Input 2: 3123 | 3124 | 99999 3125 | 3126 | Sample Output 2: 3127 | 3128 | The tax for 99999 is 26%. That is 25999.74 dollars! 3129 | ''' 3130 | income = int(input()) 3131 | percent = 0 3132 | if 0 <= income <= 15527: 3133 | percent = 0 3134 | elif 15528 <= income <= 41707: 3135 | percent = 15 3136 | elif 42708 <= income <= 85414: 3137 | percent = 22 3138 | elif 85415 <= income <= 132406: 3139 | percent = 26 3140 | else: 3141 | percent = 28 3142 | 3143 | calculated_tax = income * (percent / 100) 3144 | print(f'The tax for {income} is {percent}%. That is {calculated_tax:.2f} dollars!') 3145 | 3146 | # Python → Data types and operations → Basic string methods 3147 | 3148 | ''' 3149 | Basic string methods → Markdown parsing 3150 | 3151 | Markdown syntax is used to format a text. There are several ways to emphasize it using Markdown: 3152 | Typeface Example 3153 | Italic text *italics*, _same italics_ 3154 | Bold text **bold**, __same bold__ 3155 | Strikethrough text ~~crossed out~~ 3156 | Code snippet `code` 3157 | 3158 | Read an input and parse the text so that it doesn't include special symbols mentioned in the table "*_~`" at its beginning and its end. 3159 | 3160 | Do not forget to print the result. 3161 | 3162 | Sample Input 1: 3163 | 3164 | **Important info** 3165 | 3166 | Sample Output 1: 3167 | 3168 | Important info 3169 | 3170 | Sample Input 2: 3171 | 3172 | ~~hidden ~ tilde~~ 3173 | 3174 | Sample Output 2: 3175 | 3176 | hidden ~ tilde 3177 | ''' 3178 | markdown = input() 3179 | print(markdown.lstrip("*_~`").rstrip("*_~`")) 3180 | 3181 | ''' 3182 | Basic string methods → Preprocessing 3183 | 3184 | Preprocess an input text: 3185 | 3186 | delete punctuation symbols (commas, periods, exclamation and question marks ,.!?), 3187 | convert all symbols to lowercase. 3188 | 3189 | Then print your text. 3190 | 3191 | Punctuation marks appear not only at the end of the input string, so you have to figure out how to get rid of all of them. 3192 | 3193 | Sample Input 1: 3194 | 3195 | Nobody expects the Spanish inquisition! 3196 | 3197 | Sample Output 1: 3198 | 3199 | nobody expects the spanish inquisition 3200 | ''' 3201 | input_text = input() 3202 | input_text = input_text.replace(",", "") 3203 | input_text = input_text.replace(".", "") 3204 | input_text = input_text.replace("!", "") 3205 | input_text = input_text.replace("?", "") 3206 | input_text = input_text.lower() 3207 | print(input_text) 3208 | 3209 | ''' 3210 | Basic string methods → Poster artist 3211 | 3212 | Imagine that you design film posters for a living. Write a program that prints each film title in all caps. 3213 | 3214 | Sample Input 1: 3215 | 3216 | the lion king 3217 | 3218 | Sample Output 1: 3219 | 3220 | THE LION KING 3221 | 3222 | Sample Input 2: 3223 | 3224 | MaTRiX 3225 | 3226 | Sample Output 2: 3227 | 3228 | MATRIX 3229 | ''' 3230 | print(input().upper()) 3231 | 3232 | # Python → Collections → Set 3233 | 3234 | ''' 3235 | Set → Letters 3236 | 3237 | Write a program that calculates how many unique letters the input word has. The word is stored in the variable word. Print out the result you'll get. 3238 | 3239 | Sample Input 1: 3240 | 3241 | mississippi 3242 | 3243 | Sample Output 1: 3244 | 3245 | 4 3246 | ''' 3247 | word = input() # the input word 3248 | print(len(set(word))) 3249 | 3250 | ''' 3251 | Set → Mystery set 3252 | 3253 | There is an unknown set of objects named mystery_set which has been defined beforehand. You don't have access to this set. 3254 | 3255 | Write a program that deletes the input string (stored in the variable string) from the mystery_set. It is NOT guaranteed that the string is an element of the mystery_set to begin with. Don't forget to take that into account! 3256 | 3257 | Don't print anything! 3258 | ''' 3259 | # mystery_set has been defined 3260 | string = input() 3261 | 3262 | # delete string from mystery_set 3263 | mystery_set.discard(string) 3264 | 3265 | ''' 3266 | Set → Memory test 3267 | 3268 | Today you are assisting in a psychological experiment. To test a short-term memory, a researcher gives a set of numbers to each volunteer and asks to repeat all of them in any order. Repeats are allowed in the answer, but not the numbers not mentioned earlier. 3269 | 3270 | The input has been read into two variables for you. 3271 | 3272 | If the volunteer remembers the numbers correctly, print True, otherwise, you should output False. 3273 | 3274 | Sample Input 1: 3275 | 3276 | 1 2 3 4 5 3277 | 5 4 1 2 1 3 3278 | 3279 | Sample Output 1: 3280 | 3281 | True 3282 | 3283 | Sample Input 2: 3284 | 3285 | 999 1 1001 15 6 7 3286 | 1 6 7 1 999 3287 | 3288 | Sample Output 2: 3289 | 3290 | False 3291 | 3292 | Sample Input 3: 3293 | 3294 | 3 4 5 7 3295 | 3 4 5 6 7 3296 | 3297 | Sample Output 3: 3298 | 3299 | False 3300 | ''' 3301 | numbers = input().split() 3302 | answers = input().split() 3303 | 3304 | print(set(numbers) == set(answers)) 3305 | 3306 | ''' 3307 | Set → Counting unique 3308 | 3309 | Imagine you have information about the three students: which subjects they study. It could be in the following format: 3310 | 3311 | Belov = ["Physics", "Math", "Russian"] 3312 | Smith = ["Math", "Geometry", "English"] 3313 | Sarada = ["Japanese", "Math", "Physics"] 3314 | 3315 | The subjects can be the same or differ. Your task is to find the amount of unique subjects. 3316 | 3317 | 3318 | 3319 | NB! Don't write the prompting code. In this task you're given the variables to work: Belov, Smith, and Sarada. 3320 | 3321 | Sample Input 1: 3322 | 3323 | {"Belov": ["Physics", "Math", "Russian"], "Smith": ["Math", "Geometry", "English"], "Sarada": ["Japanese", "Math", "Physics"]} 3324 | 3325 | Sample Output 1: 3326 | 3327 | 6 3328 | ''' 3329 | # please, work with the variables 'Belov', 'Smith', and 'Sarada' 3330 | unique_subjects = set() 3331 | for i in range(0, len(Belov)): 3332 | unique_subjects.add(Belov[i]) 3333 | for i in range(0, len(Smith)): 3334 | unique_subjects.add(Smith[i]) 3335 | for i in range(0, len(Sarada)): 3336 | unique_subjects.add(Sarada[i]) 3337 | 3338 | print(len(unique_subjects)) 3339 | 3340 | # Python → Control flow statements → For loop 3341 | 3342 | ''' 3343 | For loop → A mean of n 3344 | 3345 | Write a program that reads an integer number n. This is a number of integer values you will receive on the next n lines. Your program should compute their mean value. 3346 | 3347 | Print the mean as a float number. 3348 | 3349 | Sample Input 1: 3350 | 3351 | 8 3352 | 5 3353 | -7 3354 | 6 3355 | 2 3356 | 5 3357 | 5 3358 | -7 3359 | -10 3360 | 3361 | Sample Output 1: 3362 | 3363 | -0.125 3364 | ''' 3365 | n = int(input()) 3366 | total = 0 3367 | 3368 | for i in range(0, n): 3369 | total += int(input()) 3370 | 3371 | print(total / n) 3372 | 3373 | ''' 3374 | For loop → Speech generation 3375 | 3376 | Here is your chance to write instructions for a text-to-speech system. Let's focus on reading phone numbers aloud. The input phone number will consist solely of digits. Print the name of each digit on a new line for the system to read them one by one. 3377 | 3378 | You can store the digit names in a container, e.g. in a list with the names from 0 to 9 digits = ['zero', 'one', 'two', ..., 'nine'], and refer to each digit by index, digits[2] equals to 'two', etc. Mind the type, an index should be an interger, not a string. 3379 | 3380 | Sample Input 1: 3381 | 3382 | 4901825 3383 | 3384 | Sample Output 1: 3385 | 3386 | four 3387 | nine 3388 | zero 3389 | one 3390 | eight 3391 | two 3392 | five 3393 | ''' 3394 | number = input() 3395 | digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] 3396 | 3397 | for char in number: 3398 | print(digits[int(char)]) 3399 | 3400 | ''' 3401 | For loop → Lucky 7 3402 | 3403 | Find all numbers that can be divided by 7 and print their squares. 3404 | 3405 | The input format: 3406 | 3407 | In the first line, there's an integer number of values n. 3408 | 3409 | Then you'll receive n lines with one number on each. 3410 | 3411 | The output format: 3412 | 3413 | m lines with the squares of all numbers that can be divided by 7. 3414 | 3415 | Sample Input 1: 3416 | 3417 | 5 3418 | 8 3419 | 11 3420 | -49 3421 | 0 3422 | 3564 3423 | 3424 | Sample Output 1: 3425 | 3426 | 2401 3427 | 0 3428 | ''' 3429 | n = int(input()) 3430 | for _i in range(0, n): 3431 | x = int(input()) 3432 | if x % 7 == 0: 3433 | print(x * x) 3434 | 3435 | ''' 3436 | For loop → Arithmetic mean 3437 | 3438 | Calculate the arithmetic mean of a list. The arithmetic mean is a sum of all elements divided by their total number. 3439 | 3440 | Elements are given in the list numbers. Find their sum, divide it by the number of elements and then print the result. 3441 | 3442 | Sample Input 1: 3443 | 3444 | 10 15 1 6 3 3445 | 3446 | Sample Output 1: 3447 | 3448 | 7.0 3449 | ''' 3450 | numbers = [int(x) for x in input().split()] 3451 | total = 0 3452 | for i in numbers: 3453 | total += i 3454 | print(total / len(numbers)) 3455 | 3456 | ''' 3457 | For loop → The average of all numbers 3458 | 3459 | Write the code that reads 2 numbers from the keyboard, a and b. As an output, it shows the average of all numbers that lie on the interval between a and b, and can be divided by 3. 3460 | 3461 | What does it mean? In the example, you are to calculate the average of the numbers in the range [−5;12] [-5; 12] [−5;12] 3462 | 3463 | The numbers divided by 3 without the remainder are: −3,0,3,6,9,12 -3, 0, 3, 6, 9, 12 −3,0,3,6,9,12. There are six of them, and the average is 4.5. 3464 | 3465 | The input interval always contains at least one dividend of 3. Remember to include the values of a and b in your calculations. 3466 | 3467 | Sample Input 1: 3468 | 3469 | -5 3470 | 12 3471 | 3472 | Sample Output 1: 3473 | 3474 | 4.5 3475 | ''' 3476 | # put your python code here 3477 | a = int(input()) 3478 | b = int(input()) 3479 | total = 0 3480 | counter = 0 3481 | for i in range(a, b + 1): 3482 | if i % 3 == 0: 3483 | total += i 3484 | counter += 1 3485 | print(total / counter) 3486 | 3487 | ''' 3488 | For loop → Vowel count 3489 | 3490 | Have a look at the program that iterates through string and counts vowels in it. Fill in the blank to make it work. 3491 | ''' 3492 | string = "red yellow fox bite orange goose beeeeeeeeeeep" 3493 | vowels = 'aeiou' 3494 | count = 0 3495 | 3496 | # fix this for loop 3497 | for letter in string: 3498 | if letter in vowels: 3499 | count += 1 3500 | 3501 | ''' 3502 | For loop → FizzBuzz 3503 | 3504 | FizzBuzz is a famous code challenge used in interviews to test basic programming skills. It's time to write your own implementation. 3505 | 3506 | Print numbers from 1 to 100 inclusively following these instructions: 3507 | 3508 | if a number is multiple of 3, print "Fizz" instead of this number 3509 | if a number is multiple of 5, print "Buzz" instead of this number 3510 | for numbers that are multiples of both 3 and 5, print "FizzBuzz" 3511 | print the rest of the numbers unchanged. 3512 | 3513 | Output each value on a separate line. 3514 | ''' 3515 | for i in range(1, 101): 3516 | if i % 3 == 0 and i % 5 == 0: 3517 | print("FizzBuzz") 3518 | elif i % 3 == 0: 3519 | print("Fizz") 3520 | elif i % 5 == 0: 3521 | print("Buzz") 3522 | else: 3523 | print(i) 3524 | 3525 | # Python → Control flow statements → Loop control statements 3526 | 3527 | ''' 3528 | Loop control statements → Small scale 3529 | 3530 | Read lines with floats from the input until you get a single period . that signals you to stop. 3531 | 3532 | Find the minimum of these numbers and print it out. 3533 | 3534 | Sample Input 1: 3535 | 3536 | 11.1 3537 | 2.05 3538 | 4.0 3539 | . 3540 | Sample Output 1: 3541 | 3542 | 2.05 3543 | ''' 3544 | num_list = [] 3545 | user_input = input() 3546 | while user_input != ".": 3547 | num_list.append(float(user_input)) 3548 | user_input = input() 3549 | print(min(num_list)) 3550 | 3551 | ''' 3552 | Loop control statements → Count up the squares 3553 | 3554 | Write a program that reads from the console numbers (one in a line) until their sum is equal to 0. Immediately after that, it should display the sum of the squares of all the entered numbers. 3555 | 3556 | It is guaranteed that at some point the sum of the entered numbers will be equal to 0. After that, reading is not necessary to continue. 3557 | 3558 | In case the first integer equals to 0, also stop reading values from the input. Print out 0 instead of the sum of the squares. 3559 | 3560 | For example, we are reading the numbers 1, -3, 5, -6, -10, 13. At this point, we have noticed that the sum of these numbers is 0 and output the sum of their squares, not paying attention to the fact that there are still unread values. 3561 | 3562 | Sample Input 1: 3563 | 3564 | 1 3565 | -3 3566 | 5 3567 | -6 3568 | -10 3569 | 13 3570 | 4 3571 | -8 3572 | Sample Output 1: 3573 | 3574 | 340 3575 | ''' 3576 | # put your python code here 3577 | user_input = int(input()) 3578 | _sum = user_input 3579 | squared_sum = user_input * user_input 3580 | 3581 | while _sum != 0: 3582 | user_input = int(input()) 3583 | _sum += user_input 3584 | squared_sum += user_input * user_input 3585 | print(squared_sum) 3586 | 3587 | ''' 3588 | Loop control statements → Prime number 3589 | 3590 | Determine whether the input number is a prime number. 3591 | 3592 | The output options: 3593 | 3594 | This number is prime 3595 | 3596 | This number is not prime 3597 | 3598 | Sample Input 1: 3599 | 3600 | 6 3601 | Sample Output 1: 3602 | 3603 | This number is not prime 3604 | ''' 3605 | user_input = int(input()) 3606 | is_prime = True 3607 | 3608 | for i in range(2, user_input): 3609 | if user_input % i == 0: 3610 | is_prime = False 3611 | break 3612 | 3613 | if is_prime: 3614 | print("This number is prime") 3615 | else: 3616 | print("This number is not prime") 3617 | 3618 | ''' 3619 | Loop control statements → Process integer input 3620 | 3621 | Write a program that reads integers from the console, one number per line. 3622 | 3623 | For each input number you should check: 3624 | 3625 | if the number is less than 10, then skip this number; 3626 | if the number is greater than 100, then stop reading numbers from the console; 3627 | in other cases, print this number back to the console on a separate line. 3628 | Sample Input 1: 3629 | 3630 | 12 3631 | 4 3632 | 2 3633 | 58 3634 | 112 3635 | Sample Output 1: 3636 | 3637 | 12 3638 | 58 3639 | Sample Input 2: 3640 | 3641 | 101 3642 | Sample Output 2: 3643 | 3644 | Sample Input 3: 3645 | 3646 | 1 3647 | 2 3648 | 102 3649 | Sample Output 3: 3650 | 3651 | ''' 3652 | # put your python code here 3653 | user_input = int(input()) 3654 | 3655 | while user_input: 3656 | if user_input > 100: 3657 | break 3658 | if user_input >= 10: 3659 | print(user_input) 3660 | user_input = int(input()) 3661 | ''' 3662 | Loop control statements → Palindrome 3663 | 3664 | Palindrome is a word or a text that reads the same backward as forward. Create a program that checks if the word is palindrome. 3665 | 3666 | The input format: 3667 | 3668 | Word that needs to be checked. It is guaranteed that the word will be of even length. 3669 | 3670 | The output format: 3671 | 3672 | If the word is palindrome, write Palindrome. Otherwise, write Not palindrome 3673 | 3674 | Sample Input 1: 3675 | 3676 | noon 3677 | Sample Output 1: 3678 | 3679 | Palindrome 3680 | Sample Input 2: 3681 | 3682 | banana 3683 | Sample Output 2: 3684 | 3685 | Not palindrome 3686 | ''' 3687 | # put your python code here 3688 | user_input = input() 3689 | is_palindrome = True 3690 | for i in range(len(user_input) // 2): 3691 | if user_input[i] != user_input[len(user_input) - 1 - i]: 3692 | is_palindrome = False 3693 | break 3694 | if is_palindrome: 3695 | print("Palindrome") 3696 | else: 3697 | print("Not palindrome") 3698 | 3699 | ''' 3700 | Loop control statements → Vowels and consonants 3701 | 3702 | You'll get a sequence of characters (a word, a sentence or just gibberish). For each character tell if it's a vowel or a consonant. If you hit a non-alphabetic symbol, just stop processing. 3703 | 3704 | The input format: 3705 | 3706 | One line with N characters. Alphabetic symbols will be from English alphabet. 3707 | 3708 | The output format: 3709 | 3710 | Print "vowel" if the character is a vowel, and "consonant" if the character is consonant. Stop printing information at the first non-alphabetic symbol. 3711 | 3712 | Sample Input 1: 3713 | 3714 | somegibberish 3715 | Sample Output 1: 3716 | 3717 | consonant 3718 | vowel 3719 | consonant 3720 | vowel 3721 | consonant 3722 | vowel 3723 | consonant 3724 | consonant 3725 | vowel 3726 | consonant 3727 | vowel 3728 | consonant 3729 | consonant 3730 | Sample Input 2: 3731 | 3732 | normal phrase 3733 | Sample Output 2: 3734 | 3735 | consonant 3736 | vowel 3737 | consonant 3738 | consonant 3739 | vowel 3740 | consonant 3741 | Sample Input 3: 3742 | 3743 | weusedtowritewithnospaces 3744 | Sample Output 3: 3745 | 3746 | consonant 3747 | vowel 3748 | vowel 3749 | consonant 3750 | vowel 3751 | consonant 3752 | consonant 3753 | vowel 3754 | consonant 3755 | consonant 3756 | vowel 3757 | consonant 3758 | vowel 3759 | consonant 3760 | vowel 3761 | consonant 3762 | consonant 3763 | consonant 3764 | vowel 3765 | consonant 3766 | consonant 3767 | vowel 3768 | consonant 3769 | vowel 3770 | consonant 3771 | ''' 3772 | user_input = input() 3773 | vowel = "aeiou" 3774 | consonant = "bcdfghjklmnpqrstvwxyz" 3775 | 3776 | for char in user_input: 3777 | if char in vowel: 3778 | print("vowel") 3779 | elif char in consonant: 3780 | print("consonant") 3781 | else: 3782 | break 3783 | 3784 | ''' 3785 | Loop control statements → Game over 3786 | 3787 | In online test games, there is usually a limited number of lives: if, for example, you make 3 mistakes, you lose and do not continue with the game. Imagine you are trying to implement that system to an existing online test that doesn't have it yet. 3788 | 3789 | The input format: 3790 | 3791 | A line with N scores of a user in a test game separated by a space: C for the correct answer and I for the incorrect answer. N will be between 15 and 50. 3792 | 3793 | The output format: 3794 | 3795 | If the user loses the game, print "Game over" and their score (how many correct answers they gave). If the user wins, print "You won" and their score. The message should be printed without quotation marks. The message and the score should be printed on separate lines. 3796 | 3797 | Sample Input 1: 3798 | 3799 | C C C I C C C C I I C C C C C C C C C 3800 | Sample Output 1: 3801 | 3802 | Game over 3803 | 7 3804 | Sample Input 2: 3805 | 3806 | C C I I C C C C C C C C C C C 3807 | Sample Output 2: 3808 | 3809 | You won 3810 | 13 3811 | ''' 3812 | scores = input().split() 3813 | # put your python code here 3814 | correct_score = 0 3815 | incorrect_score = 0 3816 | for score in scores: 3817 | if incorrect_score == 3: 3818 | break 3819 | if score == "C": 3820 | correct_score += 1 3821 | if score == "I": 3822 | incorrect_score += 1 3823 | if incorrect_score == 3: 3824 | print("Game over") 3825 | else: 3826 | print("You won") 3827 | print(correct_score) 3828 | 3829 | ''' 3830 | Loop control statements → Party time 3831 | 3832 | James is hosting a party today. He decided to welcome all new guests personally. To remember their names, James kindly asks you to write them in a list. Read the names from the input, each on a new line, and stop at a single period . that denotes the end of the sequence. 3833 | 3834 | Print your list and its length (the number of guests) for James. 3835 | 3836 | Sample Input 1: 3837 | 3838 | Katja 3839 | Adam 3840 | Eva 3841 | Nicholas 3842 | . 3843 | Sample Output 1: 3844 | 3845 | ['Katja', 'Adam', 'Eva', 'Nicholas'] 3846 | 4 3847 | ''' 3848 | user_input = input() 3849 | names_list = [] 3850 | while user_input != ".": 3851 | names_list.append(user_input) 3852 | user_input = input() 3853 | print(names_list) 3854 | print(len(names_list)) 3855 | 3856 | ''' 3857 | Loop control statements → The mean 3858 | 3859 | Calculate the arithmetic mean of integer numbers. You will receive the integers on separate lines. The numeric sequence ends with a period ., so stop reading the input on it. 3860 | 3861 | There will always be at least one number. 3862 | ''' 3863 | user_input = input() 3864 | names_list = [] 3865 | while user_input != ".": 3866 | names_list.append(int(user_input)) 3867 | user_input = input() 3868 | print(sum(names_list) / len(names_list)) 3869 | 3870 | ''' 3871 | Loop control statements → Cat cafés 3872 | 3873 | Kitty wants to visit a cat café! Help her find the one with the largest number of cats. 3874 | 3875 | Input format 3876 | 3877 | Each string contains a café's name followed by a space and the number of cats in it, e.g. Paws 11, Kittens 9. 3878 | 3879 | The names would be one-word only. Read input lines until you get a string MEOW (without any number). 3880 | 3881 | Output format 3882 | 3883 | The café with the maximum number of cats. 3884 | 3885 | Hint 3886 | 3887 | Sample Input 1: 3888 | 3889 | Paws 11 3890 | Kittens 9 3891 | MEOW 3892 | Sample Output 1: 3893 | 3894 | Paws 3895 | ''' 3896 | user_input = input() 3897 | cafe_list = {} 3898 | nums_list = [] 3899 | cafe = "" 3900 | num_of_cats = 0 3901 | 3902 | while user_input != "MEOW": 3903 | cafe = user_input.split()[0] 3904 | num_of_cats = int(user_input.split()[1]) 3905 | nums_list.append(num_of_cats) 3906 | cafe_list.update({num_of_cats: cafe}) 3907 | user_input = input() 3908 | 3909 | print(cafe_list.get(max(nums_list))) 3910 | 3911 | 3912 | # Python → Simple programs → Errors 3913 | 3914 | ''' 3915 | Errors → Chaos 3916 | 3917 | Look at the code below and eliminate the chaos: the first line should print the resulting number of the calculation and the second line should print the word mathematics. 3918 | ''' 3919 | print(45 / 9 + 16 * (5 + 8)) 3920 | print("mathematics") 3921 | 3922 | ''' 3923 | Errors → English contractions 3924 | 3925 | Lucy is drawing a poster with English contractions for her students. We will join her English club and help with learning materials. 3926 | 3927 | Print the following strings with contractions: 3928 | I am = I'm 3929 | I have = I've 3930 | I will = I'll 3931 | I had / would = I'd 3932 | 3933 | Each string should be on a new line. This will be easy if you use quotes wisely. 3934 | ''' 3935 | print("I am = I'm") 3936 | print("I have = I've") 3937 | print("I will = I'll") 3938 | print("I had / would = I'd") 3939 | 3940 | ''' 3941 | Errors → Visual poetry 3942 | 3943 | Just look at this piece of poetry in the shape of an egg! Try to run this code without errors. 3944 | 3945 | Please do NOT edit the strings within print(). 3946 | ''' 3947 | print(" * * * ") 3948 | print(" * * ") 3949 | print(" * Which * ") 3950 | print(" * came first: * ") 3951 | print("* the chicken *") 3952 | print(" * or the * ") 3953 | print(" * egg? *") 3954 | print(" * * * ") 3955 | 3956 | ''' 3957 | Errors → Locate a problem 3958 | 3959 | The code snippet below needs debugging. Examine it and write down the line number on which the compilation of code will stop. 3960 | 3961 | print( "The quick brown fox" ) # 1 3962 | print() # 2 3963 | print'jumps') # 3 3964 | pint() # 4 3965 | print( ' over the lazy dog ) # 5 3966 | ''' 3967 | # Python → Collections → List comprehension 3968 | 3969 | ''' 3970 | List comprehension → Vowels 3971 | 3972 | Read a string from the input and print a list of vowels that belong to that string. 3973 | 3974 | All vowels are enlisted in a variable of the same name. 3975 | 3976 | Sample Input 1: 3977 | 3978 | invertebrate 3979 | Sample Output 1: 3980 | 3981 | ['i', 'e', 'e', 'a', 'e'] 3982 | ''' 3983 | vowels = 'aeiou' 3984 | # create your list here 3985 | user_input = input() 3986 | print([i for i in user_input if i in vowels]) 3987 | 3988 | ''' 3989 | List comprehension → Even numbers 3990 | 3991 | Write a program that takes a list of numbers, creates another list of even numbers from the first list, and prints it. 3992 | 3993 | E.g. if my_numbers = [1, 2, 3, 4, 5], then your ptogram should return the list [2, 4]. 3994 | 3995 | NB: the list with numbers my_numbers is already defined, so you don't have to work with the input. 3996 | ''' 3997 | # work with the variable 'my_numbers' 3998 | print([i for i in my_numbers if i % 2 == 0]) 3999 | 4000 | ''' 4001 | List comprehension → A list of words 4002 | 4003 | Write a program that takes a list with words, creates a new list of words that start with the letter "a" in the first list, and prints it. 4004 | 4005 | Some words may begin with the capital A! Leave them in their original form in the resulting list. 4006 | 4007 | E.g. if words = ['apple', 'pear', 'banana', 'Ananas'], then your program should return the list ['apple', 'Ananas']. 4008 | 4009 | The list with words is already defined: you can access it using the variable words. 4010 | ''' 4011 | # work with the preset variable `words` 4012 | print([x for x in words if x.startswith('a') or x.startswith('A')]) 4013 | 4014 | ''' 4015 | List comprehension → How many days? 4016 | 4017 | The list seconds is a list of numbers that represent seconds. Convert the number of seconds to full days and print the list that contains these values. The number of full days should be an int value. 4018 | ''' 4019 | seconds = [86400, 3600, 1028397, 8372891, 219983, 865779330, 3276993204380912] 4020 | # create a list of days here 4021 | days = [x // 86400 for x in seconds] 4022 | print(days) 4023 | 4024 | ''' 4025 | List comprehension → Length 4026 | 4027 | Given a list of words in the code below, create a list of lengths of those words and print it. 4028 | ''' 4029 | words = ["apple", "it", "creek", "pelican", "subsequent", "horse", 4030 | "apothecary"] 4031 | print([len(w) for w in words]) 4032 | 4033 | ''' 4034 | List comprehension → Plus one 4035 | 4036 | You are given a list of strings containing integer numbers. Print the list of their values increased by 1. 4037 | 4038 | E.g. if list_of_strings = ["36", "45", "99"], your program should print the list [37, 46, 100]. 4039 | 4040 | The variable list_of_strings is already defined, you don't need to work with the input. 4041 | ''' 4042 | # please work with the variable 'list_of_strings' 4043 | print([int(x) + 1 for x in list_of_strings]) 4044 | 4045 | # Python → Collections → Nested lists 4046 | 4047 | ''' 4048 | Nested lists → Fill the blanks 4049 | 4050 | Below you can see the code that chooses some elements from one list and appends them to another: 4051 | 4052 | for a in x: 4053 | for el in a: 4054 | if el > 0: 4055 | els.append(el) 4056 | Fill in the blanks in the code below so that list comprehension produces the same result as the code above. 4057 | ''' 4058 | els = [el for a in x for el in a if el > 0] 4059 | 4060 | ''' 4061 | Nested lists → A lot of nested lists 4062 | 4063 | How to create a nested list [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]] in one line? 4064 | ''' 4065 | my_list = [[x for x in range(1, 3)] for y in range(5)] 4066 | print(my_list) 4067 | 4068 | ''' 4069 | Nested lists → Accessing elements of a matrix 4070 | 4071 | Let's say we have a matrix M: 4072 | 4073 | M = [[34, 77, 8, 45], 4074 | [80, 15, 23, 57], 4075 | [92, 86, 19, 38]] 4076 | How to print the element in the first column and the third row? 4077 | ''' 4078 | # the variable M is already defined 4079 | print(M[2][0]) 4080 | 4081 | ''' 4082 | Nested lists → Conditions & nested lists 4083 | 4084 | This is a list of students and their grades for an exam: 4085 | 4086 | students = [["Will", "B"], ["Kate", "B"], ["Max", "A"], ["Elsa", "C"], ["Alex", "B"], ["Chris", "A"]] 4087 | Select only students with the best grade ("A") and print their names in a list. Do all this in one line. 4088 | ''' 4089 | # the list "students" is already defined 4090 | print([s[0] for s in students if s[1] == "A"]) 4091 | 4092 | ''' 4093 | Nested lists → Word list 4094 | 4095 | Create a list of words from the text below that are shorter than or equal to the input value. Print the new list. 4096 | 4097 | Sample Input 1: 4098 | 4099 | 1 4100 | Sample Output 1: 4101 | 4102 | ['a', 'a'] 4103 | ''' 4104 | text = [["Glitch", "is", "a", "minor", "problem", "that", "causes", "a", "temporary", "setback"], 4105 | ["Ephemeral", "lasts", "one", "day", "only"], 4106 | ["Accolade", "is", "an", "expression", "of", "praise"]] 4107 | user_input = int(input()) 4108 | print([w for words in text for w in words if len(w) <= user_input]) 4109 | 4110 | ''' 4111 | Nested lists → A very nested list 4112 | 4113 | Write a program that takes three strings as input and then constructs and prints a nested list from them – with first string as a first element, a list containing only second string as a second element and a list containing another list containing a third string as a third element. 4114 | 4115 | Sample Input 1: 4116 | 4117 | cat 4118 | dog 4119 | dragon 4120 | Sample Output 1: 4121 | 4122 | ['cat', ['dog'], [['dragon']]] 4123 | ''' 4124 | str_1 = input() 4125 | str_2 = input() 4126 | str_3 = input() 4127 | nest_nest_list = [str_1, [str_2], [[str_3]]] 4128 | print(nest_nest_list) 4129 | 4130 | # Python → Data types and operations → Type casting 4131 | 4132 | ''' 4133 | Type casting → Exactly 100 times 4134 | 4135 | Jane knows that variable n stores some integer number (for example, 12345) and wants to print it exactly 100 times. Help her and write down a single line of code that will print number n exactly 100 times. 4136 | ''' 4137 | n = 12345 4138 | # put your python code here 4139 | print(str(n) * 100) 4140 | 4141 | ''' 4142 | Type casting → Lexical reduplication 4143 | 4144 | The languages of the world are amazing! Programming languages too, but now we will talk about the human ones. In linguistics, repeating a word or part of it is called reduplication. This morphological phenomenon is found in different languages. Think for a second, and you will definitely come up with a couple of examples. Just to name a few: knock-knock, so-so, bye-bye. 4145 | 4146 | We have a full reduplication here since the entire word is repeated. That's the mechanism we want you to implement. Print a word exactly 2 times. The spelling rules vary across the globe, so do not separate the halves (that will be easy!). 4147 | 4148 | Sample Input 1: 4149 | 4150 | maru 4151 | Sample Output 1: 4152 | 4153 | marumaru 4154 | Sample Input 2: 4155 | 4156 | piga 4157 | Sample Output 2: 4158 | 4159 | pigapiga 4160 | Sample Input 3: 4161 | 4162 | knock 4163 | Sample Output 3: 4164 | 4165 | knockknock 4166 | ''' 4167 | word = input() 4168 | 4169 | # Change the next line 4170 | print(word * 2) 4171 | 4172 | # Theory: Escape sequences 4173 | 4174 | ''' 4175 | Escape sequences → Find the length 4176 | 4177 | Write a program that will print the length of the string 'That is \n mine'. 4178 | ''' 4179 | print(len('That is \n mine')) 4180 | 4181 | ''' 4182 | Escape sequences → Good programmer 4183 | 4184 | You are really good at what you do (programming), so your colleague has decided to congratulate you on the completed project with a message "You are the best programmer!". But how to do this? 4185 | 4186 | Write the code that prints this message using an escape sequence so that each word of the sentence would be on a new line. 4187 | 4188 | Omit the trailing spaces, we can do without them! 4189 | ''' 4190 | print("You\nare\nthe\nbest\nprogrammer!") 4191 | 4192 | ''' 4193 | Escape sequences → Print a sign 4194 | 4195 | Print two backslash characters \\. 4196 | ''' 4197 | # put your python code here 4198 | print("\\\\") 4199 | 4200 | # Python → Functions → Scopes 4201 | 4202 | ''' 4203 | Scopes → Cities 4204 | 4205 | Imagine you've created a program that plays the cities game with a user. For the game to work, you need to remember the user's city and be able to change it. Below is the code that does that, but there's a mistake in it. Find it and fix it! 4206 | ''' 4207 | user_city = "Istanbul" 4208 | 4209 | def change_city(new_user_city): 4210 | global user_city 4211 | user_city = new_user_city 4212 | 4213 | change_city("Paris") 4214 | print(user_city) 4215 | 4216 | # Python → Data types and operations → Split and join 4217 | 4218 | ''' 4219 | Split and join → Prepositional genitive 4220 | 4221 | Advanced input() handling is used to read input directly into several variables, for example: 4222 | 4223 | x, y = input().split() 4224 | Use it to print the next message with the two input values: "{x} of {y}" 4225 | 4226 | Sample Input 1: 4227 | 4228 | state Massachusetts 4229 | Sample Output 1: 4230 | 4231 | state of Massachusetts 4232 | Sample Input 2: 4233 | 4234 | Queen Scots 4235 | Sample Output 2: 4236 | 4237 | Queen of Scots 4238 | ''' 4239 | x, y = input().split() 4240 | print(x + str(" of ") + y) 4241 | 4242 | ''' 4243 | Split and join → Straight A's 4244 | 4245 | Write a program that calculates the proportion of students who received grade A. 4246 | 4247 | A five-point rating system with grades A, B, C, D, F is used. 4248 | 4249 | Input format: 4250 | A string with students' marks separated by space. At least one mark will be present. 4251 | 4252 | Output format: 4253 | A fractional number with exactly two decimal places. 4254 | 4255 | To format the decimal places use the round(number, places) function. 4256 | 4257 | Hint 4258 | 4259 | Sample Input 1: 4260 | 4261 | F B A A B C A D 4262 | Sample Output 1: 4263 | 4264 | 0.38 4265 | Sample Input 2: 4266 | 4267 | B C B 4268 | Sample Output 2: 4269 | 4270 | 0.0 4271 | Sample Input 3: 4272 | 4273 | A D 4274 | Sample Output 3: 4275 | 4276 | 0.5 4277 | ''' 4278 | # put your python code here 4279 | grades = input().split() 4280 | total = 0 4281 | a_total = 0 4282 | for i in grades: 4283 | if i == "A": 4284 | a_total += 1 4285 | total += 1 4286 | print(round(a_total / total, 2)) 4287 | 4288 | ''' 4289 | Split and join → Find positions 4290 | 4291 | Write a program that reads a sequence of numbers from the first line and the number x from the second line. Then it should output all positions of x in the numerical sequence. 4292 | 4293 | The position count starts from 0. In case if x is not in the sequence, print the line "not found" (quotes omitted, lowercased). 4294 | 4295 | Positions should be displayed in one line, in ascending order of the value. 4296 | 4297 | Hint 4298 | 4299 | Sample Input 1: 4300 | 4301 | 5 8 2 7 8 8 2 4 4302 | 8 4303 | Sample Output 1: 4304 | 4305 | 1 4 5 4306 | Sample Input 2: 4307 | 4308 | 5 8 2 7 8 8 2 4 4309 | 10 4310 | Sample Output 2: 4311 | 4312 | not found 4313 | ''' 4314 | # put your python code here 4315 | seq = input().split() 4316 | find = input() 4317 | indexes = [] 4318 | counter = 0 4319 | for i in seq: 4320 | if i == find: 4321 | indexes.append(str(counter)) 4322 | counter += 1 4323 | if len(indexes) == 0: 4324 | print("not found") 4325 | else: 4326 | print(" ".join(indexes)) 4327 | 4328 | ''' 4329 | Split and join → Spellchecker 4330 | 4331 | Write a spellchecker that tells you which words in the sentence are spelled incorrectly. Use the dictionary in the code below. 4332 | 4333 | The input format: 4334 | 4335 | A sentence. All words are in the lowercase. 4336 | 4337 | The output format: 4338 | 4339 | All incorrectly spelled words in the order of their appearance in the sentence. If all words are spelled correctly, print OK. 4340 | 4341 | Sample Input 1: 4342 | 4343 | srutinize is to examene closely and minutely 4344 | Sample Output 1: 4345 | 4346 | srutinize 4347 | examene 4348 | Sample Input 2: 4349 | 4350 | all correct 4351 | Sample Output 2: 4352 | 4353 | OK 4354 | ''' 4355 | 4356 | dictionary = ['all', 'an', 'and', 'as', 'closely', 'correct', 'equivocal', 4357 | 'examine', 'indication', 'is', 'means', 'minutely', 'or', 'scrutinize', 4358 | 'sign', 'the', 'to', 'uncertain'] 4359 | seq = input().split() 4360 | incorrect = [] 4361 | for word in seq: 4362 | if word not in dictionary: 4363 | incorrect.append(word) 4364 | if len(incorrect) == 0: 4365 | print("OK") 4366 | else: 4367 | for w in incorrect: 4368 | print(w) 4369 | 4370 | ''' 4371 | Split and join → Find words 4372 | 4373 | Find all words that end in "s" and print them together separated by an underscore. 4374 | 4375 | Sample Input 1: 4376 | 4377 | First ladies rule the State and state the rule: ladies first 4378 | Sample Output 1: 4379 | 4380 | ladies_ladies 4381 | ''' 4382 | seq = input().split() 4383 | to_print = [] 4384 | 4385 | for word in seq: 4386 | if word.endswith("s"): 4387 | to_print.append(word) 4388 | 4389 | print("_".join(to_print)) 4390 | 4391 | ''' 4392 | Split and join → String tricks 4393 | 4394 | Examine the code and fix the mistakes so that the join() method works. 4395 | ''' 4396 | random_numbers = [1, 22, 333, 4444, 55555] 4397 | str_numbers = [str(n) for n in random_numbers] 4398 | print("\n".join(str_numbers)) 4399 | 4400 | ''' 4401 | Split and join → Fix the mistakes 4402 | 4403 | The code below is supposed to find all website addresses (https://, http://, www.) in the input text. However, it is not finished and there are several mistakes in the code. Find the mistakes, fix them and return all addresses in the chronological order each on a new line. 4404 | 4405 | Sample Input 1: 4406 | 4407 | WWW.GOOGLE.COM uses 100-percent renewable energy sources and www.ecosia.com plants a tree for every 45 searches! 4408 | Sample Output 1: 4409 | 4410 | WWW.GOOGLE.COM 4411 | www.ecosia.com 4412 | ''' 4413 | text = input() 4414 | words = text.split() 4415 | for word in words: 4416 | # finish the code here 4417 | if word.lower().startswith("www.") \ 4418 | or word.lower().startswith("https://") \ 4419 | or word.lower().startswith("http://"): 4420 | print(word) 4421 | 4422 | # Python → Builtins → Any and all 4423 | 4424 | ''' 4425 | Any and all → Heads or Tails 4426 | 4427 | We have tossed a coin 6 times and saved the results in a list called heads_or_tails. The values are integers: 1 stands for a head, while 0 denotes a tail. 4428 | 4429 | Add some code to find out whether the list has any heads. 4430 | ''' 4431 | # Fingers crossed 4432 | check = any(heads_or_tails) 4433 | 4434 | ''' 4435 | Any and all → Lottery 4436 | 4437 | Imagine that you have bought a bunch of lottery tickets and wrote down their numbers into the list. Now, it's time to figure out whether you have a winning ticket. 4438 | 4439 | You know that all winning tickets are no less than 44. Fill the empty fields in the code (these ones ...) to check if you have at least one winning ticket. 4440 | 4441 | You DON'T need to print the answer. 4442 | ''' 4443 | # As luck would have it 4444 | tickets = [11, 22, 33, 44, 55] 4445 | winning_tickets = [i >= 44 for i in tickets] 4446 | tickets_bool = any(winning_tickets) 4447 | 4448 | ''' 4449 | Any and all → Competition 4450 | 4451 | Today you are taking part in the chess competition. The winner of the competition will get the 'winner' status and the largest amount of money if they win all the games. Much is at stake! 4452 | 4453 | The results are stored in a list. Fill the blanks in the code below and figure out how much money you won. 4454 | 4455 | You DON'T need to print the answer. 4456 | ''' 4457 | check = any([True, True, 1, 1, True]) 4458 | 4459 | if check: 4460 | status = 'winner' 4461 | else: 4462 | status = 'looser' 4463 | 4464 | if status == 'winner': 4465 | winning_sum = 100 4466 | else: 4467 | winning_sum = 10 4468 | 4469 | # Python → Object-oriented programming → Class 4470 | 4471 | ''' 4472 | Class → The Creator 4473 | 4474 | Imagine for a second that you're a God and create a class Human. Humans are a species called "Homo Sapiens" and they (usually) have 2 legs and 2 arms. Create the attributes species, n_legs, and n_arms for your class Human. 4475 | ''' 4476 | # create a Human here 4477 | class Human: 4478 | species = "Homo Sapiens" 4479 | n_legs = 2 4480 | n_arms = 2 4481 | 4482 | ''' 4483 | Class → The housing problem 4484 | 4485 | The code below defines the class House and an object of that class. However, the code is somewhat incorrect. Fix the mistakes. 4486 | ''' 4487 | class House: 4488 | construction = "building" 4489 | elevator = True 4490 | 4491 | 4492 | h = House() 4493 | 4494 | ''' 4495 | Class → Who is who 4496 | 4497 | There are two classes: Angel and Demon. 4498 | 4499 | They have certain characteristics that help tell them apart. Both of these classes have 3 common class attributes with different values: 4500 | 4501 | class Angel: 4502 | color = "white" 4503 | feature = "wings" 4504 | home = "Heaven" 4505 | 4506 | 4507 | class Demon: 4508 | color = "red" 4509 | feature = "horns" 4510 | home = "Hell" 4511 | Suppose there is a mysterious object called mystery_entity. Print its attributes to find out whether this entity is an angel or a demon. You should print the attributes in this order: color, feature, home. Each should be on a separate line. 4512 | ''' 4513 | # mystery_entity has already been defined 4514 | # print its class attributes 4515 | print(mystery_entity.color) 4516 | print(mystery_entity.feature) 4517 | print(mystery_entity.home) 4518 | 4519 | ''' 4520 | Class → Let's rock 4521 | 4522 | There are many bands in the world that perform all kinds of music. Let's suppose for a second that you're a fan of rock and want to create a program that deals with rock bands. 4523 | 4524 | For that, you obviously need the class RockBand with such attributes as genre ("rock"), n_members(4, by default) and famous_songs (an empty list as a default value). Create this class and an object of that class: name the variable after any rock band that you like. 4525 | 4526 | You do NOT need to fill the famous_songs attribute, leave the default value. 4527 | 4528 | Print the attributes of your rock band on separate lines in this order: genre, n_members, famous_songs. 4529 | ''' 4530 | # start a RockBand here 4531 | class RockBand: 4532 | genre = "rock" 4533 | n_members = 4 4534 | famous_songs = [] 4535 | 4536 | r = RockBand() 4537 | print(r.genre) 4538 | print(r.n_members) 4539 | print(r.famous_songs) 4540 | 4541 | 4542 | --------------------------------------------------------------------------------