Skip to content

Instantly share code, notes, and snippets.

@marceloandriolli
Last active February 4, 2020 14:16
Show Gist options
  • Save marceloandriolli/b3bce21601b7ea076178e8ee24f77cbe to your computer and use it in GitHub Desktop.
Save marceloandriolli/b3bce21601b7ea076178e8ee24f77cbe to your computer and use it in GitHub Desktop.
code challenge
#!/usr/bin/env python3
def count_digits(n, digit):
"""Return the number of times digit appears in the squares of the sequence 1..n.
Example:
count_digits(10, 1) = 4
# 1, 2, 3, 4, ..., 9, 10 --> 1, 4, 9, 16, ..., 81, 100
"""
number_of_digits = 0
for number in range(1, n+1):
square = number**2
if str(digit) in str(square):
number_of_digits += 1
return number_of_digits
def balancedNum(n):
"""Return 'Balanced' if `n` is a 'balanced' number, otherwise 'Not Balanced'.
Balanced number: a number that if reversed, equals itself.
Examples:
1
121
12321
"""
result = 'Not Balanced'
reversed_seq = [number for number in reversed(str(n))]
if str(n) == ''.join(reversed_seq):
result = 'Balanced'
return result
def reverse_number(n):
"""Return the reverse of any integer `n`.
Example:
1 -> 1
123 -> 321
10 -> 1
"""
reversed_seq = [number for number in reversed(str(n))]
reversed_number = int(''.join(reversed_seq))
return reversed_number
def even_weights(numbers):
"""Return True if the sum of the even members of `numbers` add up to the sum
of the odd members, otherwise return False.
Example:
1, 1 -> True
1, 2, 2, 1 -> True
"""
result = sum(numbers) % 2 == 0
return result
def fizz_slash_buzz(n):
"""Return a tuple containing the number of multiples of 3, 5, and 15 for all
integers from 1 to `n`.
If a number is a multiple of 15, do NOT count it for 3 or 5.
Example:
n = 15 -> (4, 2, 1)
"""
result = []
for number in range(1, n+1):
if number % 3 == 0 or number % 5 == 0:
result.append(number)
if number % 15 == 0:
pass
return (0, 0, 0)
# Life emulation.
#
# Make a 4 x 4 grid of square cells. Cells can be alive (1) or dead (0).
# Randomly populate the grid. Over time the grid of sells evolve. We
# determine which cells are alive in the next step of time by applying
# the following rules:
#
# 1. Any live cell with fewer than two live neighbours dies, as if
# caused by under-population.
#
# 2. Any live cell with more than three live neighbours dies, as if by
# overcrowding.
#
# 3. Any live cell with two or three live neighbours lives on to the
# next generation.
#
# 4. Any dead cell with exactly three live neighbours becomes a live
# cell, as if by reproduction.
#
# Write a program that runs the life emulation for 5 time steps.
#
# Example output:
#
# Board
# 1 1 1 0
# 0 1 0 0
# 1 1 1 1
# 0 1 0 0
# Step 1
# 1 1 1 0
# 0 0 0 1
# 1 0 0 0
# 1 1 0 0
# Step 2
# 0 1 1 0
# 1 0 1 0
# 1 1 0 0
# 1 1 0 0
# Step 3
# 0 1 1 0
# 1 0 1 0
# 0 0 1 0
# 1 1 0 0
# Step 4
# 0 1 1 0
# 0 0 1 1
# 1 0 1 0
# 0 1 0 0
# Step 5
# 0 1 1 1
# 0 0 0 1
# 0 0 1 1
# 0 1 0 0
from random import randint
from copy import deepcopy
def under_population(cell: int, neighbours: list)-> int:
if not cell:
return cell
if neighbours.count(1) < 2:
return 0
else:
return cell
def overcrowding(cell: int, neighbours: list)-> int:
if not cell:
return cell
if neighbours.count(1) > 3:
return 0
else:
return cell
def next_generation(cell: int, neighbours: list)-> int:
if not cell:
return cell
if neighbours.count(1) == 2 or neighbours.count(1) == 3:
return 1
else:
return cell
def reproduction(cell: int, neighbours: list)-> int:
if cell:
return cell
if neighbours.count(1) == 3:
return 1
else:
return cell
class LifeEmulation:
"""
A class used to emulate Life
Attributes
----------
rules : list
list of rules functions
Run Life Emulation with 5 steps
-------
life_emulation = LifeEmulation([under_population,
overcrowding,
next_generation,
reproduction])
steps = 5
for step in range(0, steps):
print(life_emulation())
"""
def __init__(self, rules: list):
"""
Parameters
----------
rules : list
list of rules functions
"""
self.rules = rules or None
self.board = self._make_board()
def _make_board(self):
"""Generate a 4x4 grind board of cells"""
grid = []
for line in range(0, 4):
line_list = []
for col in range(0, 4):
line_list.append(randint(0, 1))
grid.append(line_list)
return grid
def _perform_rules(self, cell: int, neighbours: list)-> int:
"""Apply list of rules
Parameters
----------
cell : int
value of cell
neighbours : list
list of neighbours values
Returns
-------
int
a integer that represent a result of applied rules
"""
result = 0
if not self.rules:
return cell
for rule in self.rules:
result = rule(cell, neighbours)
if result != cell:
break
return result
def _find_neighbors(self, row: int, col: int) -> list:
"""Find a neighbors
Parameters
----------
row : int
current row of matrix
col : int
current col of matrix
Returns
-------
list
a list of found neighbors
"""
neighbors = []
# get the nearest neighbor through the col
if col < len(self.board) - 1:
neighbors.append(self.board[row][col + 1])
if col > 0:
neighbors.append(self.board[row][col - 1])
# get the nearest neighbor through the row
if row < len(self.board) - 1:
neighbors.append(self.board[row + 1][col])
if row > 0:
neighbors.append(self.board[row - 1][col])
# get the nearest neighbor through right bottom corner
if col < len(self.board) - 1 and row < len(self.board) - 1:
neighbors.append(self.board[row + 1][col + 1])
#get the nearest neighbor through left bottom corner
if row < len(self.board) - 1 and col > 0:
neighbors.append(self.board[row + 1][col - 1])
# get the nearest neighbor through left top corner
if col > 0 and row > 0:
neighbors.append(self.board[row - 1][col - 1])
# get the nearest neighbor through right top corner
if col < len(self.board) - 1 and row > 0:
neighbors.append(self.board[row - 1][col + 1])
return neighbors
def __call__(self):
new_step = []
for row_index, row_data in enumerate(self.board):
row = []
for col_index, col_data in enumerate(row_data):
neighbors = self._find_neighbors(row_index, col_index)
row.append(self._perform_rules(col_data, neighbors))
new_step.append(row)
self.board.clear()
self.board = deepcopy(new_step)
return new_step
life_emulation = LifeEmulation([under_population,
overcrowding,
next_generation,
reproduction])
steps = 5
for step in range(0, 5):
print(life_emulation())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment