Python Basics Exercises Pat Water problem (iii)

Source: Internet
Author: User

#学习笔记

#用以练习python基础

#


Original title Link: https://www.patest.cn/contests/pat-b-practise/1039


1039. Buy or not buy (20)

Little Red want to buy some beads to make a string of their favorite beads. The owner of the bead sells a lot of colorful beads, but refuses to sell any strings. So little Red want you to help judge, whether a bead contains all the beads they want? If so, tell her how many extra beads there are, and if not, tell her how many beads are missing.

For convenience, we use characters in [0-9], [A-z], [A-z] range to represent colors. For example, in Figure 1, the 3rd string is the bead string that little red wants to do, so the 1th string can be bought, because it contains all the beads she wants, and 8 more unwanted beads; The 2nd string cannot be bought because there is no black beads and a red bead is missing.

650) this.width=650; "src=" http://nos.patest.cn/gx_nkqhj1eeck3.jpg "style=" margin:0px;padding:0px;border:0px; Font-style:inherit;font-variant:inherit;font-weight:inherit;font-size:inherit;line-height:inherit;font-family: inherit;vertical-align:baseline; "alt=" gx_nkqhj1eeck3.jpg "/>
Figure 1

Input format:

Each input consists of 1 test cases. Each test case is given in 2 rows of the owner's bead string and Little red want to do the bead string, two strings are not more than 1000 beads.

Output format:

If you can buy, output "Yes" in one line and how many extra beads are available, and if not, output "no" in a row and how many beads are missing. Separated by 1 spaces in between.

Input Sample 1:

Ppryygrrybr2258yrr8rry

Output Example 1:

Yes 8

Input Sample 2:

Ppryygrryb225yrr8rry

Output Example 2:

No 2

AC Code

L=list (Input ()) N=list (input ()) X=len (n) t=-1for i in range (len (N)): Try:if L.index (N[t]) >=0:l.pop ( L.index (N[t]) x=x-1 n.pop () except:t=t-1if len (N) ==0:print ("Yes", Len (L)) Else:pri NT ("No", X)




Original title Link: https://www.patest.cn/contests/pat-b-practise/1054

1054. Averaging (20)

The basic requirement of the subject is very simple: given n real numbers, calculate their average. But the complex is that some of the input data may be illegal. A "valid" input is a real number within the [ -1000,1000] interval and is up to 2 digits after the decimal point. When you calculate the average, you can't count the illegal data.

Input format:

Enter the first line to give the positive integer n (<=100). The next line gives the n real numbers, separated by a space between the numbers.

Output format:

For each illegal input, output "error:x is not a legal number" in a row, where X is the input. Finally, the result is output in a row: "The average of K numbers is Y", where K is the number of legal inputs, Y is their average value, accurate to 2 digits after the decimal point. If the average cannot be calculated, replace Y with "Undefined". If k is 1, then output "The average of 1 number is Y".

Input Sample 1:

75-3.2 AAA 9999 2.3.4 7.123 2.35

Output Example 1:

ERROR:AAA is not a legal numbererror:9999 are not a legal numbererror:2.3.4 are not a legal numbererror:7.123 are not a l Egal numberthe average of 3 numbers is 1.38

Input Sample 2:

2aaa-9999

Output Example 2:

ERROR:AAA is not a legal numbererror: -9999 are not a legal numberthe average of 0 numbers are Undefined


AC Code

N=int (Input ()) L=list (str (input ())). Split ('   ')) Sum1=0x=0for i in range (N):     try:        if float (L[i]) >=-1000 and float (L[ I]) <=1000 and len (str (float (l[i))). Split ('. ') [1])  <=2:            sum1=sum1+float (L[i])             x=x+1         else:            print (' ERROR: ', L[i], ' is not a legal number ')     except:         print (' ERROR: ', l[i], ' is not a legal number ') if x==0:     print (' the average of 0 numbers is undefined ') elif x==1:     print (' the average of 1 number is %0.2f '  %  (sum1)) Else:    print (' the average of %d  numbers is %.2f '  %  (x,sum1/x))




Original title Link: https://www.patest.cn/contests/pat-b-practise/1012


1012. Digital Classification (20)

Given a series of positive integers, classify the numbers as required and output the following 5 numbers:

  • A1 = The number of all even numbers that can be divisible by 5;

  • A2 = will be replaced by 5 after the remainder 1 of the number in the order of the staggered summation, that is, calculate n1-n2+n3-n4 ... ;

  • A3 = number of digits remaining 2 after being removed by 5;

  • A4 = The average of the number of the remainder 3 after 5, accurate to 1 digits after the decimal point;

  • A5 = The largest number in the number of the remaining 4 after 5 is removed.

    Input format:

    Each input consists of 1 test cases. Each test case first gives a positive integer of not more than 1000 n, followed by a N of not more than 1000 of the positive integers to be classified. The numbers are separated by a space.

    Output format:

    For a given n positive integer, the A1~A5 is computed by the title and sequentially output in a row. The numbers are separated by a space, but there is no extra space at the end of the line.

    If one of the numbers does not exist, the "N" is output at the corresponding location.

    Input Sample 1:

    13 1 2 3 4 5 6 7 8 9 10 20 16 18

    Output Example 1:

    30 11 2) 9.7 9

    Input Sample 2:

    8 1 2 4 5 6 7 9 16

    Output Example 2:

    N 2 N 9
Import mathl=list (str (input ())). Split ('   ')) L.pop (0) a1=[]a3=[]a4=[]a5=[]sum1=0k=0max1=-1q=0try:     for i in l:        if i.isalnum ( ) ==true :            if int (i)%5==0  and int (i)  % 2 ==0:                 a1.append (int (i))              if int (i)%5==1 :                 sum1=int (Sum1+int (i) *math.pow ( -1,k))                  k=k+1             if int (i)  % 5 == 2 :                  a3.append (i)              if int (i)  % 5 == 3:                 a4.append (int (i))              if int (i)  % 5 == 4:                 if int (i)  > max1:                     max1=int (i)     if len (A1)  ==0:        a5.append (' N ' )     else:        a5.append (sum (A1))          if k ==0:         A5.append (' N ')   &nbSp; else:        a5.append (SUM1)          if len (A3)  ==0:        a5.append (' N ')     else:        a5.append (Len (A3))          if len (A4)  ==0:         A5.append (' N ')     else:        a5.append (round (sum (A4)/len (A4), 1)         if max1==-1:         a5.append (' N ')     else:         a5.append (MAX1)     print (a5[0],a5[1],a5[2],a5[3],a5[4]) except:     print (' N n n n n ')


This article is from the "Chauncey" blog, make sure to keep this source http://cqwujiang.blog.51cto.com/10808946/1919691

Python Basics Exercises Pat Water problem (iii)

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.