Introduction to computer science and programming (3) Design and implementation of iterative programs, traversal, and tuples

Source: Internet
Author: User

1. Iterative Program Design: Basic Elements

The implementation of iterative programs usually includes the following five parts:

  • Counter (Count): used to mark the number of cycles
  • Initialization: Initialize outside the loop
  • Right End Test: When the End is usually closely related to the Count
  • Code block of the loop part: This part of the code block usually contains changes to the counter
  • What should the program do after the end?
2. Iterative Program Design: Flowchart

You can use flowcharts to organize programs before using specific code.

For example, if the square of a positive integer is X, this positive integer is required. You can set this positive integer to ans.

 

The Code is as follows:

# Find the square root of a perfect square 1.0x = 16ans = 0while ans*ans < x:    ans = ans + 1print(ans)

Of course, the above Code still has a lot of room for improvement. You can make slight modifications to increase the judgment on x.

# Find the square root of a perfect square 2.0x = 16ans = 0if x>=0:    while ans*ans <x:        ans = ans + 1        print('ans =',ans)    if ans*ans != x:        print(x,'is not a perfect square')    else:        print(ans)else:    print(x, 'is a negative number')

In the improved code, we make different judgments for the absence of input values of x. This method of designing a program is calledDefense Program Design.

Defense Program Design: Make sure that all user inputs correspond to different code paths.

3. General loop mode: Traversal

The loop we have previously considered is mainly an incremental loop. In fact, it can be expanded to a more general situation, that isAccess each element in the Set in sequenceWe call thisTraversal

For element in Set

Code block

Which of the following numbers can be divisible by 10 to 10 (excluding 10)? First, consider the incremental method.

 

# Calculate the number of divisible values between 1 and 10 (excluding 10) x = 10 count = 1 while (count <x): if (x % count = 0 ): print ('divisor ', count) count + = 1 # python does not support count ++

 

Next, we will consider the method of traversing the set elements.

 

# Use a set traversal to calculate the number x = 10 that can be divisible by 10 between 1 and 10 # The range function generates an integer between 1 and 10, excluding 10for I in range (1, x): if x % I = 0: print ('divisor ', I)

 

Through comparison, we can find that the following benefits can be achieved by traversing through the for Loop:

 

4. tuples

Continue to consider the divisor example. Now we need to aggregate the displayed divisor so that we can perform other operations. In this case, we can consider using tuples.

Tuples are a seriesOrderedAnd isImmutableOf

The creation and display of elements are very simple. use parentheses.

# Create and display tuples test = (1, 3, 5, 4, 2) test [0] #=> 1 test [2] #=> 5 test [-1] #=> 2 test [-6] #=> 1 test [-7] # => wrongtest [5] #=> wrongtest [] #=> (3, 5) test [2:] #=> (5, 4, 2) test [: 3] #=> (1, 3, 5)

Next, we can place the qualified divisor in the same tuples.

# Store numbers that can be divisible by 100 between 1 and 100 in the same tuples x = divisors = () for I in range (1, x ): if x % I = 0: divisors = divisors + (I,) # if I is not followed by a comma, python treats it as an integer to display an error.

 

 

 

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.