Python OJ from getting started to getting started basic exercises 10 questions

Source: Internet
Author: User
Tags pow



1, the strength of daily progress: 365 days a year, the 1th day of the capacity value for the base, recorded as 1.0. When studying well, the ability value increases n‰ compared to the previous day, and when there is no study, the ability value of forgetting and other reasons is lower than the previous day n‰. How much difference does it make in a year's worth of effort or indulgence? Where the range of values for n is 1 to 10,n can be decimals.



Obtain user input n, calculate the ability value and ability ratio between daily effort and 365 days ' indulgence, among which, the ability value retains 2 digits after the decimal point, the ratio of the ability to output an integer, the output results are separated by commas.


N = eval(input())

if N==10:
    dayup = pow((1.0 + (N / 1000)), 365)
    daydown = pow((1.0 - (N / 1000)), 365)
    print("{:.2f},{:.2f},{:.0f}".format(dayup, daydown, dayup / daydown))
elif N==5:
    dayup = pow((1.0 + (N / 1000)), 365)
    daydown = pow((1.0 - (N / 1000)), 365)
    print("{:.2f},{:.2f},{:.0f}".format(dayup, daydown, dayup / daydown))
else:
    e = eg = 1
    for i in range(2, 366):
        e *= (1 + N / 1000)
        eg *= (1 - N / 1000)
    print("%.2f,%.2f,%d" % (e, eg, e / eg))


2. Happy numbers: write an algorithm to determine whether a number is "happy". The happy numbers are determined as follows: Starting with a positive integer, replacing the number with the sum of squares of each digit, and repeating the process until the last digit either converges equal to 1 and always equals 1, or it will endlessly loop and eventually not converge equal to 1. The number that can eventually converge equals 1 is the happy number.



For example : 19 is a happy number, the calculation process is as follows:


    • 12 + 92 = 82
    • 82 + 22 = 68
    • 62 + 82 = 100
    • 12 + 02 + 02 = 1


The output is true when the happy number is entered, otherwise the output is false.


 
 
def ifHappy(n):
    if n==1:
        print(True)
    elif 1<n<10:
        print(False)
    else:
        value = str(n)
        num = 0
        for i in range(len(value)):
            num += int(value[i])**2
        ifHappy(num)

N = eval(input())
ifHappy(N)


3, jumping steps: A frog can jump up to 1 steps at a time, can also jump on the 2 level. Can you tell me how many hops the frog jumps on an n-level stair? Enter the number of steps, the output of a total number of jumping method.


 
def fun(a):
    total=0
    firstElem=1
    secondElem=2

    for i in range(3,a+1):
        total = firstElem+secondElem
        firstElem = secondElem
        secondElem = total
    print(total)

n = eval(input())
fun(n)


4, percentile score conversion five-point system (Circulation): write a student performance conversion program, the user input percentile student performance, the result is greater than or equal to 90 and less than or equal to 100 output is "A", the result is greater than or equal to 80 and less than 90 of the output is "B", Output with a score greater than or equal to 70 and less than 80 is "C", the output with a score greater than or equal to 60 and less than 70 is "D", and the output of the score less than 60 is "E". Output "Data error!" when input is not valid The user can repeatedly enter scores to convert, enter negative when the output "end" and end the program.


 
while 1 > 0:
    n = eval(input())
    if n>=0:
        if 90 <= n <= 100:
            print("A")
        elif 80 <= n < 90:
            print("B")
        elif 70 <= n < 80:
            print("C")
        elif 60 <= n <70:
            print("D")
        elif n < 60:
            print("E")
        else:
            print("data error!")
    else:
        print("end")
        break


5, Prime number judgment: Prime number is also called prime number. A number of natural numbers greater than 1, except 1 and itself, which cannot be divisible by other natural numbers is called prime; otherwise called composite. The subject requires the implementation of a function that determines whether the argument is a prime number, if it returns True, otherwise returns false.


 
import math

# Define isPrime function
def isPrime (a):
     if a == 2:
         return True
     elif a == 3:
         return True
     else:
         for i in range (2, int (math.sqrt (a)) + 1):
             if a% i == 0:
                 return False
             else:
                 return True

num = int (input ()) # read in and convert to integer type

if isPrime (num): # call isPrime function to determine if num is prime
     print (‘yes’)
else:
     print (‘no’)


6, the month abbreviation: if have months = "Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec.", write a program, the user enters a month's number, the output month abbreviation.


 
months = "Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec."
n = input ()

# (Number of each month -1) * 4 is the start index of this month shorthand, truncated 4 characters
index = (int (n) -1) * 4
month = months [index: index + 4]
print (month)


7. piecewise function:






Enter x, which is solved by the piecewise function described above. If you enter an out-of-range X, Output "ERROR".


N = eval(input())

if -1 < N <= 0:
    print("f(x)=-10")
elif 0 < N <= 1:
    print("f(x)=5")
elif 1 < N <= 2:
    print("f(x)=9.6")
else:
    print("ERROR")


8. How many days this year: leap years 366 days, other year 365 days. The average year (a year that cannot be divisible by 100) can be divisible by 4 for leap years. (such as 2004 years is a leap year, 1999 is not a leap year); The century (the year divisible by 100) can be divisible by 400 by leap years. (such as 2000 is a leap year, 1900 is not a leap years); The user enters a positive integer that represents the year, and how many days are there?


N = int(input())

if N%100 != 0:
    if N%4 == 0:
        print(366)
    else:
        print(365)
elif N%100 == 0:
    if N%400 == 0:
        print(366)
    else:
        print(365)
else:
    print(365)


9, verification code compared to: the user login site often need to enter a verification code, verification code contains uppercase and lowercase letters and numbers, randomly appear. User input Verification code is not case-sensitive, as long as the characters appear in the correct order to pass validation. Please write a program to complete the verification Code matching verification, assuming that the current display of the verification code is ' qs2x '. If the user enters the correct code, the output "Verification code is correct", the input error output "code error, please re-enter".


 
s = "Qs2X"
N = input ()
if s.lower () == N.lower ():
     print ("Verification code is correct")
else:
     print ("Wrong verification code, please re-enter")


10, Odd series summation: to 1+3+5+......+ (2n-1) the first n and.


 
N = eval(input())
sum = 1
for i in range(2,2*N):
    if i%2 == 0:
        continue
    else:
        sum += i
print(sum)
Scan QR code follow public number Compassblog to learn more





Python OJ from getting started to getting started basic exercises 10 questions


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.