Home > Backend Development > Python Tutorial > Collatz sequences, comma codes, character map grids

Collatz sequences, comma codes, character map grids

巴扎黑
Release: 2017-06-23 11:44:05
Original
1680 people have browsed it

1.collatz sequence

Write a function named collatz(), which has a parameter named number. If the argument is an even number, then collatz() prints number // 2 and returns that value. If number is odd, collatz() prints and returns 3 * number + 1. Then write a program that lets the user input an integer and continuously calls collatz() on this number until the function returns a value of 1.

 1 #!/usr/bin/env python3 2 # -*- coding:utf-8 -*- 3  4 def collatz(number): 5     print(number) 6     if number ==1: 7         return number 8     elif number % 2 ==0: 9         return collatz(number//2)10     else:11         return collatz(3*number +1)12 13 A = int(input('Input a number: '))14 while True:15     if collatz(A) != 1:16         continue17     else:18         break
Copy after login

Output result:

 1 Input a number: 6 2 6 3 3 4 10 5 5 6 16 7 8 8 4 9 210 1
Copy after login

2. Comma code

Assume that there is a list like the following: spam = [' apples', 'bananas', 'tofu', 'cats'] <br> Write a function that takes a list value as a parameter and returns a string. The string contains all entries separated by commas and spaces, with and inserted before the last entry. For example, passing the previous spam list to the function will return 'apples, bananas, tofu, and cats'. But your function should be able to handle any list passed to it.

<br>
Copy after login
#!/usr/bin/env python3<br># -*- coding:utf-8 -*-<br><br>def func(spam):<br>    spam[-1]='and'+ ' ' + spam[-1]<br>for i in range(len(spam)):<br>print(spam[i], end=',')<br><br><br>spam = ['apple', 'bananas', 'tofu', 'cats', 'dog']<br>func(spam)<br>#输出结果<br>apple,bananas,tofu,cats,and dog,<br>
Copy after login

3. Character graph grid

Suppose there is a list of lists, and each value of the inner list is a string containing one character, like this :

grid =[['.', '.', '.', '.', '.', '.'],

['.', 'O' , 'O', '.', '.', '.'],

['O', 'O', 'O', 'O', '.', '.'],

['O', 'O', 'O', 'O', 'O', '.'],

['.', 'O', 'O' , 'O', 'O', 'O'],

['O', 'O', 'O', 'O', 'O', '.'],

['O', 'O', 'O', 'O', '.', '.'],

['.', 'O', 'O', '.' , '.', '.'],

['.', '.', '.', '.', '.', '.']]

you can It is considered that grid[x][y] is the characters at the x and y coordinates of a "picture", which is composed of text characters. The origin (0, 0) is in the upper left corner, the x coordinate increases to the right, and the y coordinate increases downward. Copy the previous grid value and write code to print the image using it.

..OO.OO..

.OOOOOOO.

.OOOOOOO.

..OOOOO..

.. .OOO...

....O....

    grid =     [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,  #嵌套循环  n       m          (grid[m][n], end=     ()#换行

#输出结果
..OO.OO..

.OOOOOOO.

.OOOOOOO.

..OOOOO..

...OOO...

....O....
Copy after login

<br>

The above is the detailed content of Collatz sequences, comma codes, character map grids. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template