**HackerRank
ps.python
Day 0
# Read a full line of input from stdin and save it to our dynamically typed variable, input_string.
input_string = input()
# Print a string literal saying "Hello, World." to stdout.
print('Hello, World.')
print(input_string)
# TODO: Write a line of code here that prints the contents of input_string to stdout.
- LOOPS
The provided code stub reads and integer, , from STDIN. For all non-negative integers n, print ii .*
if __name__ == '__main__':
n = int(input())
if n<1 or n>20:
print('1<=n<=20')
for i in range(0,n) :
print(i*i)
Day 1
i = 4
d = 4.0
s = 'HackerRank '
# Declare second integer, double, and String variables.
i1 = int(input())
d1 = float(input())
s1 = str(input())
print(i+i1)
print(d+d1)
print(s+s1)
# Read and save an integer, double, and String to your variables.
# Print the sum of both integer variables on a new line.
# Print the sum of the double variables on a new line.
# Concatenate and print the String variables on a new line
# The 's' variable above should be printed first.
- Write a function
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.
def is_leap(year):
leap = False
if year in range(1900,pow(10,5)+1):
if year % 4 == 0:
if year % 100 != 0:
leap = True
elif year % 400 == 0:
leap = True
# Write your logic here
return leap
year = int(input())
print(is_leap(year))
Day 2: Operators
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the solve function below.
def solve(meal_cost, tip_percent, tax_percent):
totalCost = tip = tax = 0
tip=tip_percent*meal_cost/100
tax = tax_percent*meal_cost/100
totalCost =meal_cost + tip + tax
return int(round(totalCost))
if __name__ == '__main__':
meal_cost = float(input())
tip_percent = int(input())
tax_percent = int(input())
totalCost = solve(meal_cost, tip_percent, tax_percent)
print(totalCost)
- Print Function
例如:输入n=5,输出1234
if __name__ == '__main__':
n = int(input())
[print(i+1, end = '') for i in range(n)]
#Day3 : Intro to Conditional Statements
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input())
if n<1 or n>100:
print('1<=n<=100')
if n % 2 != 0 :
print('Weird')
if n % 2 == 0 and n in range(2,6) :
print ('Not Weird')
if n %2 == 0 and n in range (6,21):
print('Weird')
if n%2==0 and n>20:
print('Not Weird')
#不知道为啥子,合在一起写就有错误
Day 4: Class vs. Instance
class Person:
def __init__(self,initialAge):
# Add some more code to run some checks on initialAge
if initialAge<0:
print("Age is not valid, setting age to 0.")
else :
self