PAT 1084 appearance sequence Python solution

Source: Internet
Author: User

A sequence of appearances refers to a series of integers with the following characteristics:
D, D1, d111, d113, d11231, d112213111, ...
It never equals the number D of 1, and the n+1 item of the sequence is a description of the nth item. For example, the 2nd means that the 1th item has 1 d, so it is D1;
The 2nd item is 1 d (corresponds to D1) and a 1 (corresponding to 11), so the 3rd item is d111. Another example is the 4th item is d113, its description is a d,2 1, a 3,
So the next item is d11231. Of course this definition is also set for d = 1. The subject asks you to calculate the nth of the appearance sequence for any given number d.
Input format:
Enter the first line to give an integer d in the [0,9] range, and a positive integer N (≤40), separated by a space.
Output format:
The nth item in a row that gives the appearance of an array of number d.
Input Sample:
1 8
Sample output:
1123123111

My solution is mainly to construct a string d=d+ ".", so as to ensure that when judging d[i]==d[i+1],
The subscript does not overflow, and avoids the "." The problem is equal to the number.
Then the iterative method can be used to do it. This problem began to think for a long time, want to use a recursive method to solve,
But I think it's too complicated, and then after a while, find an example and analyze it separately.
D,n =input (). Split ()defFun (d): D= d+"."s=""Count= 1 forIinchRange (len (D)):ifD[i]==d[i+1]: Count+=1Else: S= s+d[i]+Str (count) Count= 1returnSN=1 whileTrue:ifn==Int (N): Break    Else: D=Fun (D) n+=1PrintD

PAT 1084 appearance sequence Python solution

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.