Write a program which asks the user for a year, and prints out the next leap year. Example:
Year: 2024
Return: The next leap year after 2024 is 2028
Write a program which asks the user for a string and then prints out a frame of * characters with the word in the centre. The width of the frame should be 30 characters. You may assume the input string will always fit inside the frame. If the length of the input string is an odd number, you may print out the word in either of the two possible centre locations. Example:
Word: python
Return:
******************************
* python *
******************************
Write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. Example:
Please type in a number: 2
Return:
1 x 1 = 1
1 x 2 = 2
2 x 1 = 2
2 x 2 = 4
Write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range. Example:
Please type in a number: 5
Return:
1
5
2
4
3
Write a function named chessboard, which prints out a chessboard made out of ones and zeroes. The function takes an integer argument, which specifies the length of the side of the board. Example:
chessboard(5)
Return:
10101
01010
10101
01010
10101
Write a function named shape, which takes 4 arguments. The first two parameters specify a triangle, and the character used to draw it. The 1st parameter also specifies the width of a rectangle, while the 3rd parameter specifies its height. The 4th parameter specifies the filler character of the rectangle. The function prints first the triangle, and then the rectangle below it. Example:
shape(5, "X", 3, "*")
Return:
X
XX
XXX
XXXX
XXXXX
*****
*****
*****
Write a function named spruce, which takes one argument. The function prints out the text a spruce!, and the a spruce tree, the size of which is specified by the argument. Example:
spruce(4)
Return:
a spruce!
*
***
*****
*******
*
Write a function named anagrams, which takes two strings as arguments. The function returns True if the strings are anagrams of each other. Two words are anagrams if they contain exactly the same characters (case-sensitive in this case). Example:
print(anagrams("tame", "meta")) #True
print(anagrams("python", "java")) #False
Write a function named palindromes, which takes a string argument and returns True if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards (case-sensitive). Also write a main program which asks the user to type in words until they type in a palindrome. Example:
Please type in a palindrome: python
that wasn't a palindrome
Please type in a palindrome: Avijit
that wasn't a palindrome
Please type in a palindrome: racecar
racecar is a palindrome!
Write a function named shortest, which takes a list of strings as its argument. The function returns whichever of the strings is the shortest. If more than one are equally short, the function can return any of the shortest strings (there will be no such situation in the tests). You may assume there will be no empty strings in the list. Example:
my_list = ["adele", "mark", "dorothy", "tim", "hedy", "richard"]
result = shortest(my_list)
print(result)
Return: tim
Write a function named all_the_longest, which takes a list of strings as its argument. The function should return a new list containing the longest string in the original list. If more than one are equally long, the function should return all of the longest strings. The order of the strings in the returned list should be the same as in the original. Example:
my_list = ["adele", "mark", "dorothy", "tim", "hedy", "richard"]
result = all_the_longest(my_list)
print(result)
Return: ['dorothy', 'richard']
Write a function named most_common_character, which takes a string argument. The function returns the character which has the most occurrences within the string. If there are many characters with equally many occurrences, the one which appears first in the string should be returned. Example:
second_string = "exemplaryelementary"
print(most_common_character(second_string))
Return: e
In a game of Go two players take turns to place black and white stones on a game board. The winner is the player who manages to encircle a bigger area on the board with their own game pieces.
Write a function named who_won(game_board: list), which takes a two-dimensional array as its argument. The array consists of integer values, which represent the following situations:
0: empty square
1: player 1 game piece
2: player 2 game piece
The scoring rules of Go can be quite complex, but in this exercise it is enough to compare the number of pieces each player has on the game board. Also, the size of the game board is not limited.
The function should return the value 1 if player 1 won, and the value 2 if player 2 won. If both players have the same number of pieces on the board, the function should return the value 0.
Sudoku can be represented in matrix form.
A) Write a function named row_correct(sudoku: list, row_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single row, as its arguments. Rows are indexed from 0.
B) Write a function named column_correct(sudoku: list, column_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single column, as its arguments. Columns are indexed from 0.
The function should return True or False, depending on whether the row is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once. Example:
sudoku = [
[9, 0, 0, 0, 8, 0, 3, 0, 0],
[2, 0, 0, 2, 5, 0, 7, 0, 0],
[0, 2, 0, 3, 0, 0, 0, 0, 4],
[2, 9, 4, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 3, 0, 5, 6, 0],
[7, 0, 5, 0, 6, 0, 4, 0, 0],
[0, 0, 7, 8, 0, 3, 9, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 3],
[3, 0, 0, 0, 0, 0, 0, 0, 2]]
print(row_correct(sudoku, 0)) #Return True
print(column_correct(sudoku, 1)) #Return True
Write a function named double_items(numbers: list), which takes a list of integers as its argument.
The function should return a new list, which contains all values from the original list doubled. The function should not change the original list. Example:
numbers = [2, 4, 5, 3, 11, -4]
numbers_doubled = double_items(numbers)
print("original:", numbers)
print("doubled:", numbers_doubled)
Return: [2, 4, 6, 3, 5]
The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below.
The function add_number(sudoku: list, row_no: int, column_no: int, number:int) takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should add the digit to the specified location in the grid. Example:
sudoku = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
print_sudoku(sudoku)
add_number(sudoku, 0, 0, 2)
add_number(sudoku, 1, 2, 7)
add_number(sudoku, 5, 7, 3)
print()
print("Three numbers added:")
print()
print_sudoku(sudoku)
Return:
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
Three numbers added:
2 _ _ _ _ _ _ _ _
_ _ 7 _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ 3 _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _
Write a function named factorials(n: int), which returns the factorials of the numbers 1 to n in a dictionary. The number is the key, and the factorial of that number is the value mapped to it. Example:
k = factorials(5)
print(k[1])
print(k[3])
print(k[5])
Return:
1
6
120
Write a function named invert(dictionary: dict), which takes a dictionary as its argument. The dictionary should be inverted in place so that values become keys and keys become values. Example:
s = {1: "first", 2: "second", 3: "third", 4: "fourth"}
print(invert(s))
Return: {"first": 1, "second": 2, "third": 3, "fourth": 4}
A) Write a function named add_movie(database: list, name: str, director: str, year: int, runtime: int), which adds a new movie object into a movie database. The database is a list, and each movie object in the list is a dictionary. The dictionary should contain the following keys.
–> name, director, year, runtime
The values attached to these keys are given as arguments to the function. Example:
database = []
add_movie(database, "Gone with the Python", "Victor Pything", 2017, 116)
add_movie(database, "Pythons on a Plane", "Renny Pytholin", 2001, 94)
print(database)
Return: [{"name": "Gone with the Python", "director": "Victor Pything", "year": 2017, "runtime": 116}, {"name": "Pythons on a Plane", "director": "Renny Pytholin", "year": 2001, "runtime": 94}]
B) Now write a function named find_movies(database: list, search_term: str), which processes the movie database created in the (A) exercise. The function should formulate a new list, which contains only the movies whose title includes the word searched for. Capitalisation is irrelevant here. A search for ana should return a list containing both Anaconda and Management. Example:
database = [{"name": "Gone with the Python", "director": "Victor Pything", "year": 2017, "runtime": 116}, {"name": "Pythons on a Plane", "director": "Renny Pytholin", "year": 2001, "runtime": 94}, {"name": "Dawn of the Dead Programmers", "director": "M. Night Python", "year": 2011, "runtime": 101}]
my_movies = find_movies(database, "python")
print(my_movies)
Return: [{"name": "Gone with the Python", "director": "Victor Pything", "year": 2017, "runtime": 116}, {"name": "Pythons on a Plane", "director": "Renny Pytholin", "year": 2001, "runtime": 94}]
Write a function which creates passwords of a desired length, consisting of lowercase characters a to z. Example:
for i in range(2):
print(generate_password(8))
Return (could be different than below due to randomness):
lttehepy
olsxttjl
Write an improved version of your password generator. The function now takes three arguments:
If the second argument is True, the generated password should also contain one or more numbers.
If the third argument is True, the generated password should also contain one or more of these special characters: !?=+-()#.
Despite these two additional arguments, the password should always contain at least one lowercase alphabet. You may assume the function will only be called with combinations of arguments that are possible to formulate into passwords following these rules. That is, the arguments will not specify e.g. a password of length 2 which contains both a number and a special characters, for then there would not be space for the mandatory lowercase letter. Example:
for i in range(4):
print(generate_strong_password(8, True, True))
Return:
2?0n+u31
u=m4nl94
n#=i6r#(
da9?zvm?
*** Currently I am stuck here, if anyone would like to help me I would highly appreciate ***