https://repl.it/student/classrooms/17929
Statement Write a program that greets the user by printing the word "Hello", a comma, the name of the user and an exclamation mark after it. See the examples below.
Warning. Your program's output should strictly match the desired one, character by character. There shouldn't be any space between the name and the exclamation mark. You can use + operator to concatenate two strings. See the lesson for details.
Example input Harry
Example output Hello, Harry!
a = input()
print('Hello, ' + a + '!')
Statement Write a program that reads an integer number and prints its previous and next numbers. See the example below.
Example input 179
Example output The next number for the number 179 is 180 The previous number for the number 179 is 178
a = int(input())
print('The next number for the number ' + str(a) + ' is ' + str(a + 1)) print('The previous number for the number ' + str(a) + ' is ' + str(a - 1))
Statement N students take K apples and distribute them among each other evenly. The remaining (the indivisible) part remains in the basket. How many apples will each single student get? How many apples will remain in the basket?
The program reads the numbers N and K. It should print the two answers for the questions above.
Example input 6 50
Example output 8 2
n = int(input()) k = int(input())
print(k // n) print(k % n)
Statement Given the integer N - the number of seconds that is passed since midnight - how many full hours and full minutes are passed since midnight?
The program should print two numbers: the number of hours (between 0 and 23) and the number of minutes (between 0 and 1339).
For example, if N = 3900, then 3900 seconds have passed since midnight - i.e. now it's 1:05am. So the program should print 1 65 - 1 full hour is passed since midnight, 65 full minutes passed since midnight.
Example input 3900
Example output 1 65
a = int(input()) sec_on_hour = 3600
print((a // 3600 ), (a // 60))
Statement Given two timestamps of the same day: a number of hours, minutes and seconds for both of the timestamps. The moment of the first timestamp happened before the moment of the second one. Calculate how many seconds passed between them.
Example input #1 1 1 1 2 2 2
Example output #1 3661
Example input #2 1 2 30 1 3 20
Example output #2 50
hour1 = int(input()) minute1 = int(input()) second1 = int(input()) hour2 = int(input()) minute2 = int(input()) second2 = int(input())
seconds_stamp_one = ((hour1 * 3600) + (minute1 * 60) + second1) seconds_stamp_two = ((hour2 * 3600) + (minute2 * 60) + second2)
print(seconds_stamp_two - seconds_stamp_one)
Statement Given a two-digit integer, print its left digit (a tens digit) and then its right digit (a ones digit). Use the operator of integer division for obtaining the tens digit and the operator of taking remainder for obtaining the ones digit.
Example input 79
Example output 7 9
a = int(input())
print(a // 10, a % 10)
Statement Given a two-digit integer, swap its digits and print the result.
Example input 79
Example output 97
a = int(input())
print(str(a % 10)+ str(a // 10))
Statement Given an integer greater than 9, print its last two digits.
Example input 1234
Example output 34
a = int(input())
print(a % 100)
Statement Given an integer, print its tens digit.
Example input 1234
Example output 3
a = int(input())
print((a % 100) // 10 )
Statement Given a three-digit number. Find the sum of its digits.
Example input 123
Example output 6
a = int(input())
print(((a % 1000) // 100) + ((a % 100) // 10) + (a % 10))
(this was hard) Statement Given a positive real number, print its first digit to the right of the decimal point.
Example input 1.79
Example output 7
a = float(input())
print( int((a % 1)*10) // 1)
Statement A car can cover distance of N kilometers per day. How many days will it take to cover a route of length M kilometers? The program gets two numbers: N and M.
Example input 700 750
Example output 2
import math
n = int(input()) m = int(input()) print( math.ceil(m / n))
Statement Given a year (as a positive integer), find the respective number of the century. Note that, for example, 20th century began with the year 1901.
Example input 2000
Example output 20
year = (int(input()))
if ((year % 100) == 0) : print(year // 100) else: print((year // 100) + 1)
Statement Given the integer N - the number of minutes that is passed since midnight - how many hours and minutes are displayed on the 24h digital clock?
The program should print two numbers: the number of hours (between 0 and 23) and the number of minutes (between 0 and 59).
For example, if N = 150, then 150 minutes have passed since midnight - i.e. now is 2:30 am. So the program should print 2 30.
n_minutes = int(input())
hours = (n_minutes // 60) min = (n_minutes % 60)
print(hours, min)
Statement A school decided to replace the desks in three classrooms. Each desk sits two students. Given the number of students in each class, print the smallest possible number of desks that can be purchased.
The program should read three integers: the number of students in each of the three classes, a, b and c respectively.
In the first test there are three groups. The first group has 20 students and thus needs 10 desks. The second group has 21 students, so they can get by with no fewer than 11 desks. 11 desks is also enough for the third group of 22 students. So we need 32 desks in total.
Example input 20 21 22
Example output 32
students_a = int(input()) students_b = int(input()) students_c = int(input())
desk_a = (students_a // 2) if (students_a % 2 == 0) else ( (students_a // 2) + 1 ) desk_b = (students_b // 2) if (students_b % 2 == 0) else ( (students_b // 2) + 1 ) desk_c = (students_c // 2) if (students_c % 2 == 0) else ( (students_c // 2) + 1 )
print(desk_a + desk_b + desk_c)
Statement Given a three-digit integer X consisting of three different digits, print "YES" if its three digits are going in an ascending order from left to right and print "NO" otherwise.
Example input #1 179
Example output #1 YES
Example input #2 197
Example output #2 NO
number = int(input())
#obtain each number hundreds = int(number / 100) tens =int((number - (hundreds * 100)) / 10) units = int(number - (((hundreds * 100) + (tens * 10)) ))
if (hundreds < tens) and (tens < units): print('YES') else: print('NO')
__
Statement Let's call an integer a palindrome if it remains the same after reading its digits from right to left. Given a four-digit integer, print "YES" if it's a palindrome and print "NO" otherwise.
Example input #1 1221
Example output #1 YES
Example input #2 1234
Example output #2 NO
number = int(input())
#obtain each number thousands = int(number / 1000) new_number = (number - (thousands * 1000)) hundreds = int((new_number) / 100) new_number = new_number - (hundreds*100) tens = int((new_number) / 10) new_number = new_number - (tens * 10) units = new_number
if (thousands == units) and (hundreds == tens): print('YES') else: print('NO')
Statement Given three integers. Determine how many of them are equal to each other. The program must print one of the numbers: 3 (if all are same), 2 (if two of them are equal to each other and the third one is different) or 0 (if all numbers are different).
Example input 10 5 10
Example output 2
value_a = int(input()) value_b = int(input()) value_c = int(input())
equal = 1 if ((value_a == value_b) or (value_a == value_c)) else 0 equal += 1 if ((value_b == value_a) or (value_b == value_c)) else 0 equal += 1 if ((value_c == value_a) or (value_c == value_b)) else 0
print(equal)
Statement Given three integers, in which two are equal to each other and the third one is different. Print the order number of this different one - 1, 2 or 3.
Example input #1 10 5 10
Example output #1 2
Example input #2 10 10 5
Example output #2 3
input_a = int(input()) input_b = int(input()) input_c = int(input())
if (input_b == input_c) : order = 1 if (input_a == input_c) : order = 2 if (input_a == input_b) : order = 3
print(order)
Chess rook moves horizontally or vertically. Given two different squares of the chessboard, determine whether a rook can go from the first square to the second one in a single move.
The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a rook can go from the first square to the second one in a single move or NO otherwise.
x = int(input()) y = int(input()) intended_x = int(input()) intended_y = int(input())
possible_x_left = x - 1 if (x > 1) else x possible_x_right = x + 1 if (x < 8) else x
possible_y_top = y - 1 if (y > 1) else y possible_y_bottom = y + 1 if (y < 8) else y
#possibles combinations
can_move='NO' if (possible_x_left >= intended_x) and (intended_y == y) : can_move = 'YES' if (possible_x_right >= intended_x) and (intended_y == y) : can_move = 'YES' if (intended_x == x) and (intended_y >= possible_y_top) : can_move = 'YES' if (intended_x == x) and (intended_y >= possible_y_bottom) : can_move = 'YES'
if (intended_y == y) : can_move = 'YES'
if (intended_x == x) : can_move = 'YES'
print(can_move)
Statement Given a square of a chessboard. If it's a black square, print YES, otherwise print NO.
The program receives two integers from 1 to 8 specifying the column and row number of the square.
column = int(input()) row = int(input())
columnEven = True if (column % 2 == 0) else False rowEven = True if (row % 2 == 0) else False
result = 'YES' if (columnEven == rowEven) else 'NO'
print(result)
Statement Given two squares of a chessboard. If they are painted in the same color, print YES, otherwise print NO.
The program receives four integers from 1 to 8, each specifying the column and row number, first two - for the first square, and then the last two - for the second square.
columnOne = int(input()) rowOne = int(input()) columnTwo = int(input()) rowTwo = int(input())
def isEven(position): return True if (position % 2 == 0) else False
columnOneEven = isEven(columnOne) rowOneEven = isEven(rowOne) columnTwoEven = isEven(columnTwo) rowTwoEven = isEven(rowTwo)
resultOne = 'YES' if (columnOneEven == rowOneEven) else 'NO' resultTwo = 'YES' if (columnTwoEven == rowTwoEven) else 'NO'
finalResult = 'YES' if (resultOne == resultTwo) else 'NO'
print(finalResult)
Chess king moves one square in any direction. Given two different squares of the chessboard, determine whether a king can go from the first square to the second one in a single move.
The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a king can go from the first square to the second one in a single move or NO otherwise.
currentCol = int(input()) currentRow = int(input()) intendedCol = int(input()) intendedRow = int(input())
canMove = 'NO'
if ( (intendedCol == currentCol) and ((intendedRow == currentRow + 1) or (intendedRow == currentRow - 1)) ): canMove = 'YES'
if ((intendedRow == currentRow) and ((intendedCol == currentCol - 1) or (intendedCol == currentRow + 1))): canMove = 'YES'
if ((intendedCol == currentCol - 1) and (intendedRow == currentRow + 1)) : canMove = 'YES'
if ((intendedCol == currentCol + 1) and (intendedRow == currentRow + 1)): canMove = 'YES'
if ((intendedCol == currentCol - 1) and (intendedRow == currentRow - 1)): canMove = 'YES'
if ((intendedCol == currentCol + 1) and (intendedRow == currentRow - 1)): canMove = 'YES'
print(canMove)
Chess bishop moves diagonally in any number of squares. Given two different squares of the chessboard, determine whether a bishop can go from the first square to the second one in a single move.
The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a bishop can go from the first square to the second one in a single move or NO otherwise.
currentCol = int(input()) currentRow = int(input()) intendedCol = int(input()) intendedRow = int(input())
canMove = 'NO'
if ((intendedCol < currentCol) and (intendedRow > currentRow)) : canMove = 'YES'
if ((intendedCol > currentCol) and (intendedRow > currentRow)): canMove = 'YES'
if ((intendedCol < currentCol) and (intendedRow < currentRow)): canMove = 'YES'
if ((intendedCol > currentCol) and (intendedRow < currentRow)): canMove = 'YES'
print(canMove)
3.I. If/else: Knight move
Chess knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. The complete move therefore looks like the letter L. Given two different squares of the chessboard, determine whether a knight can go from the first square to the second one in a single move.
The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a knight can go from the first square to the second one in a single move or NO otherwise.
currentCol = int(input()) currentRow = int(input()) intendedCol = int(input()) intendedRow = int(input())
canMove = 'NO'
if ( (intendedCol == currentCol + 2 ) and ( (intendedRow == currentRow + 1) or (intendedRow == currentRow - 1) ) ) : canMove = 'YES'
if ( (intendedCol == currentCol - 2 ) and ( (intendedRow == currentRow + 1) or (intendedRow == currentRow - 1) ) ) : canMove = 'YES'
if ( (intendedRow == currentRow + 2 ) and ( (intendedCol == currentCol + 1) or (intendedCol== currentCol - 1) ) ) : canMove = 'YES'
if ( (intendedRow == currentRow - 2 ) and ( (intendedCol == currentCol + 1) or (intendedCol== currentCol - 1) ) ) : canMove = 'YES'
print(canMove)
Statement Given the year number. You need to check if this year is a leap year. If it is, print LEAP, otherwise print COMMON.
The rules in Gregorian calendar are as follows:
a year is a leap year if its number is exactly divisible by 4 and is not exactly divisible by 100 a year is always a leap year if its number is exactly divisible by 400
Warning. The words LEAP and COMMON should be printed all caps.
year = int(input())
isLeap = 'COMMON'
if ( ((year % 4 == 0) and (year % 100 > 0)) or (year % 400 == 0) ) : isLeap = 'LEAP'
print(isLeap)
Write a program that solves a linear equation ax = b in integers. Given two integers a and b (a may be zero), print a single integer root if it exists and print "no solution" or "many solutions" otherwise.
Example input #1 1 -2
Example output #1 -2
Example input #2 2 -1
Example output #2 no solution
a = int(input()) b = int(input())
if a != 0: x = b / a result = int(x) if x == int(x) else 'no solution' if x == 0 : result = 'no solution' else: result = 'no solution'
if (b==0): if (a == 0) : result = 'many solutions' else: result = 0
print(result)
Statement Given integer coordinates of three vertices of a rectangle whose sides are parallel to coordinate axes, find the coordinates of the fourth vertex of the rectangle.
Example input #1 1 5 7 5 1 10 three vertices are (1, 5), (7, 5), (1, 10)
Example output #1 7 10
Example input #2 1 5 7 10 1 10
Example output #2 7 5
xA = int(input()) yA = int(input()) xB = int(input()) yB = int(input()) xC = int(input()) yC = int(input())
if (xA == xB): xD = xC else: if (xB == xC): xD = xA else: xD = xB
if (yA == yB): yD = yC else: if (yB == yC): yD = yA else: yD = yB
print(xD) print(yD)
Statement Given three integers, print them in ascending order.
Example input 5 3 7
Example output 3 5 7
valueA = int(input()) valueB = int(input()) valueC = int(input())
results = []
results.append(valueA) results.append(valueB) results.append(valueC)
results.sort()
for number in results: print(number)
These are my answers to my first Python tutorial.