Hackerrank之Python练习(一)

Say "Hello, World!" With Python

print("Hello, World!")

Python If-Else

if __name__ == '__main__':
    n = int(raw_input())
    if n%2==1:
        print('Weird')
    else:
        if n>=2 and n<=5:
                print("Not Weird")
        elif n>=6 and n<=20:
                print("Weird")
        else:
                print("Not Weird") 

Arithmetic Operators

if __name__ == '__main__':
    a = int(raw_input())
    b = int(raw_input())
    print(a+b)
    print(a-b)
    print(a*b)

Python: Division

from __future__ import division
if __name__ == '__main__':
    a = int(raw_input())
    b = int(raw_input())
    print(a//b)#不保留小数
    print(a/b)
    

Loops

if __name__ == '__main__':
    n = int(raw_input())
    for i in range(n):
        print(i*i)

Write a function


def is_leap(year):
    leap = False
    if year%4==0 and year%100!=0:
        leap=True
    elif year%400==0:
        leap=True
    else:
        laep=False
    
    return leap

Print Function


from __future__ import print_function
if __name__ == '__main__':
    n = int(raw_input())
    
    for i in range(1,n+1):
        print(i,end="")
        

Power - Mod Power


# Enter your code here. Read input from STDIN. Print output to STDOUT
a=int(raw_input())
b=int(raw_input())
m=int(raw_input())
c=a**b
print(c)
print(pow(a,b,m))

Integers Come In All Sizes

# Enter your code here. Read input from STDIN. Print output to STDOUT
a=int(raw_input())
b=int(raw_input())
c=int(raw_input())
d=int(raw_input())
print(a**b+c**d)

List Comprehensions

if __name__ == '__main__':
    x = int(raw_input())
    y = int(raw_input())
    z = int(raw_input())
    n = int(raw_input())
    print[[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if (i+j+k!=n)]

Find the Runner-Up Score!

if __name__ == '__main__':
    n = int(raw_input())
    arr = map(int, raw_input().split())
    print(sorted(set(arr))[-2])
    

Nested Lists

marksheet=[]
n=int(raw_input())
for _ in range(n):
    name=raw_input()
    score=float(raw_input())
    marksheet.append([name,score])
#marksheet=[[name,score] for _ in range(0,n)]

second_lowest=sorted(set(score for name,score in marksheet))[1]
print("\n".join(name for name,score in sorted(marksheet) if score==second_lowest))

Finding the percentage


if __name__ == '__main__':
    n = int(raw_input())
    student_marks = {}
    for _ in range(n):
        line = raw_input().split()
        name, scores = line[0], line[1:]
        scores = map(float, scores)
        student_marks[name] = scores
    query_name = raw_input()
    query_scores=student_marks[query_name]
    mean=sum(query_scores)/len(query_scores)
    print("%.2f" % mean)  #print("{0:.2f}".format(mean))

Check Strict Superset

A=set(map(int,raw_input().split()))
n=int(raw_input())
set_list=[]
for _ in range(n):
    x=set(map(int,raw_input().split()))
    set_list.append(x)

result=True
for i in set_list:
    if not A.issuperset(i):
        result=False
        
print(result)
    

Lists

if __name__ == '__main__':
    N = int(raw_input())
    list=[]
    for _ in range(N):
        line=raw_input().split()
        command=line[0]
        if command=="insert":
             list.insert(int(line[1]),int(line[2]))
        elif command=="print":
             print(list)
        elif command=="remove":
             list.remove(int(line[1]))
        elif command=="append":
             list.append(int(line[1]))
        elif command=="sort":
             list.sort()
        elif command=="pop":
             list.pop()
        elif command=="reverse":
             list.reverse()










你可能感兴趣的:(Python)