(1). Arithmetic Operator
The provided code stub reads two integers from STDIN, and Add code to print three lines where:
The first line contains the sum of the two numbers.
The second line contains the difference of the two numbers (first - second).
The third line contains the product of the two numbers.
EX: a=3, b=5 would return (8, -2, 15) (+, -, *)
Constraints (a >= 1 and a <= 10**10) and (b >= 1 and b <= 10**10)
(2). If-Else
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print 'Not Weird'
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20 , print Not Weird
(3). for loop between Constraints (1 <= n >= 20)
Input 3 prints (0, 1, 4)
Input 5 prints (0, 1, 4, 9, 16)
(4). Leap Year
An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.
In the Gregorian calendar, three conditions are used to identify leap years:
a. The year can be evenly divided by 4, is a leap year, unless:
b. The year can be evenly divided by 100, it is NOT a leap year, unless:
c. The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years. Source
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.
Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.
Constraints (1900 <= year <= 10**5)
<Given Code>
def is_leap(year):
leap = False
<write your code here>
return leap
year = int(input())
print(is_leap(year))
(5). Print Function
The included code stub will read an integer, from STDIN.
Without using any string methods, try to print the following:
Example (one line no blank space)
n = 5 would print 12345
n = 3 would print 123
<Given Code>
if __name__ == '__main__':
n = int(input())
try:
<write code here>
except ValueError:
print("Error: input must be between 1 and 150")
(6). List Comperhensions
Let's learn about list comprehensions! You are given three integers x, y and z representing the dimensions of a cuboid along with an integer n. Print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i + j + k is not equal to n. Here, (0 <= i <= x); (0 <= j <= y); (0 <= k <= z) . Please use list comprehensions rather than multiple loops, as a learning exercise.
Print an array of the elements that do not sum to n = 3
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,2]]
Four integers x,y,z and n, each on a separate line.
Constraints. Print the list in lexicographic increasing order.
Example: x=1, y=1, z=2, n=3
Draw a cuboid there going to be total of 12 points.
First find all permutations of [i,j,k] are
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2]]
Use forloop to list all. Hint
for i in range(x + 1)
for j in range(y + 1)
for k in range(z + 1)
print list of coords
<Given Code>
if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
<write code here>
(7). Find the Runner up Score
Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.
The first line contains n. The second line contains an array.
Example:
5
2 3 6 6 5
Given list is [2,2,6,6,5] the maximun score is 6. second maximum is 5. Hence it would print 5
You have to sort it and take out only unique set. I didn't need to us n input
(8). Nested List
Given the names and grades for each student in a class of N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.
Note: If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line.
Example
records = [["chi",20.0],["beta",50.0],["alpha",50.0]]
The ordered list of scores is [20.0,50.0] so the secord lowest score is 50.0 There are two students with that score ["beta","alpha"] prints beta and alpha
input
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
output
Berry
Harry
n = 5
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
The lowest grade of 37.2 belongs to Tina. The second lowest grade of 37.21 belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.
(9). Finding the percentage
The provided code stub will read in a dictionary containing key/value pairs of name:[marks] for a list of students. Print the average of the marks array for the student name provided, showing 2 places after the decimal.
Example
marks key:value pairs are
'alpha':[20,30,40]
'beta':[30,50,70]
query_name = 'beta'
The query_name is 'beta' beta average score is (30 + 50 + 70)/3 = 50.0
Input Format
The first line contains the integer , the number of students' records. The next lines contain the names and marks obtained by a student, each value separated by a space. The final line contains query_name, the name of a student to query.
Constraints
2 <= n <= 10
0 <= marks[i] <= 100
length of marks arrays = 3
Output Format
Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.
Sample Input 0
3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Malika
Sample Output 0
56.00
Explanation 0
Marks for Malika are {52, 56, 60} whose average is (52+56+60)/3 => 56
Sample Input 1
2
Harsh 25 26.5 28
Anurag 26 28 30
Harsh
Sample Output 1
26.50
<Given Code>
if __name__ == '__main__':
n = int(input())
student_marks = {}
for _ in range(n):
name, *line = input().split()
scores = list(map(float, line))
student_marks[name] = scores
query_name = input()
(10). List
Consider a list (list = []). You can perform the following commands:
1. insert i e: Insert integer e at position i.
2. print: Print the list.
3. remove e: Delete the first occurrence of integer e.
4. append e: Insert integer e at the end of the list.
5. sort: Sort the list.
6. pop: Pop the last element from the list.
7. reverse: Reverse the list.
Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.
Example
N = 4
append 1
append 2
insert 1 3
* append 1: Append 1 to the list, arr = [1]
* append 2: Append 2 to the list, arr = [1,2]
* insert 1 3: insert 3 at the index 1, arr = [1,3,2]
output [1, 3, 2]
Input Format
The first line contains an integer, , denoting the number of commands.
Each line of the subsequent lines contains one of the commands described above.
Constraints
The elements added to the list must be integers.
Output Format
For each command of type print, print the list on a new line.
Sample Input
12
1. insert 0 5
2. insert 1 10
3. insert 0 6
4. print
5. remove 6
6. append 9
7. append 1
8. sort
9. print
10. pop
11. reverse
12. print
Sample Output
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
if __name__ == '__main__':
N = int(input())
(11). Time Delta
When users post an update on social media,such as a URL, image, status update etc., other users in their network are able to view this new post on their news feed. Users can also see exactly when the post was published, i.e, how many hours, minutes or seconds ago.
Since sometimes posts are published and viewed in different time zones, this can be confusing. You are given two timestamps of one such post that a user can see on his newsfeed in the following format:
Day dd Mon yyy hh:mm:ss +xxxx
Here +xxxx represents the time zone. Your task is to print the absolute difference (in seconds) between them.
Input Format
The first line contains T, the number of testcases.
Each testcase contains 2 lines, representing time t1 and time t2.
Constraints
Input contains only valid timestamps
year <= 3000
Output Format
Print the absolute difference (t1 - t2) in seconds.
Sample Input 0
2
Sun 10 May 2015 13:54:36 -0700
Sun 10 May 2015 13:54:36 -0000
Sat 02 May 2015 19:54:36 +0530
Fri 01 May 2015 13:54:36 -0000
Sample Output 0
25200
88200
Explanation 0
In the first query, when we compare the time in UTC for both the time stamps, we see a difference of 7 hours. which is 7 x 3600 seconds or 25200 seconds.
Similarly, in the second query, time difference is 5 hours and 30 minutes for time zone adjusting for that we have a difference of 1 day and 30 minutes. Or 24 x 3600 + 30 x 60 = 88200