SoFunction
Updated on 2024-10-28

Collatz Sequence, Comma Code, Character Map Grid Example

sequences

Write a function called collatz() that takes an argument called number. If the argument is an even number, 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 enter an integer and keeps calling collatz() on that number until the function returns the value 1.

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

def collatz(number):
 print(number)
 if number ==1:
  return number
 elif number % 2 ==0:
  return collatz(number//2)
 else:
  return collatz(3*number +1)

A = int(input('Input a number: '))
while True:
 if collatz(A) != 1:
  continue
 else:
  break

Output results:

Input a number: 6
6
3
10
5
16
8
4
2
1

2. Comma code

Assume a list like the following: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string containing all the list items separated by commas and spaces, with and inserted before the last list item. The string contains all the list items, separated by commas and spaces, with and inserted before the last list item. e.g. Passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to handle any list passed to it.

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

def func(spam):
 spam[-1]='and'+ ' ' + spam[-1]
 for i in range(len(spam)):
  print(spam[i], end=',')


spam = ['apple', 'bananas', 'tofu', 'cats', 'dog']
func(spam)
# Output results
apple,bananas,tofu,cats,and dog,

3. Character map grid

Assuming there is a list of lists, 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 think of grid[x][y] as the characters at the x and y coordinates of a "graph" that consists of text characters. The origin (0, 0) is in the upper left corner, and the x-coordinate increases to the right and the y-coordinate increases to the bottom. Copy the previous grid values and write code to print an image from it.

....

.OOOOOOO.

.OOOOOOO.

..OOOOO..

...OOO...

....O....

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

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', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.']]
#Nested loops
for n in range(len(grid[0])):
 for m in range(len(grid)):
  print(grid[m][n], end='')
 print('\n')# Line feeds

# Output results
....

.OOOOOOO.

.OOOOOOO.

..OOOOO..

...OOO...

....O....

This Collatz Sequence, Comma Code, Character Map Grid Example above is all I have to share with you.