Question
all Q is python only coding if you do not know answer leave it for someone else thank you Q1. The code below provides a
all Q is python only coding if you do not know answer leave it for someone else thank you
Q1.
The code below provides a function is_anagram which takes two input strings and calculates whether potential is an anagram of known_word, returning True or False.
An anagram of a string of charcters is any string containing exactly the same characters as the original string, but in a different order. 'nnaa' is an anagram of 'anna', but 'nnab' is not.
def is_anagram(potential: str, known_word: str) -> bool: """Return whether potential is an anagram of known_word Postconditions: the output is True if potential is an anagram of known_word, else False. """ if len(potential) != len(known_word) or potential == known_word: return False list_potential = list(potential) list_known_word = list(known_word) list_potential.sort() list_known_word.sort() for index in range(len(list_potential)): if list_potential[index] != list_known_word[index]: return False return True
Add four different test cases to the table below to thoroughly test the function is_anagram.
q2 Python
develop an algorithm to count the number of occurrences of a given substring in a given string. Substrings can overlap. For example, 'abababa' contains the substring 'bab' twice:
abababa
abababa
Outline in English an algorithm to count the number of occurrences of a given substring in a given string. An outline in English should not be program code or pseudo-code; see Section 6.4.1 for further guidance on outlining algorithms in English. Not using an English outline will lead to marks being deducted.
A problem definition has been provided below, which you can use to guide your solution.
Function: count substrings Inputs: main string, a string, substring, a string Preconditions: main string is a non-zero length string; substring is a non-zero length string at maximum the same length as main string Output: count, an integer
unfinished code :
def count_sub_strings(substring: str, main_string: str) -> int: pass # replace pass with your code
# Tests %run -i m269_util test_table = [ ['None','abc','def',0], ['One','abc','xabcx',1], ['Two','abc','xabcxabc',2] ] test(count_sub_strings, test_table)
Q3,
Patterns can be represented in lists (see Section 4.8.6 for an example of an image being loaded into a list). The following code takes an existing pattern defined in a list and calculates a new pattern. This example is based on the Game of Life devised by John Conway in 1970. You do not need a full understanding of the Game of Life, although more information is available on Wikipedia if you wish.
The code below accepts a 'grid' defined in a list of lists and creates a new grid based on the defined rules. The grid may be either square (with rows and columns equal in quantity) or rectangular (with dissimilar numbers of rows and columns). Note again you do not have to understand the rules of the Game of Life: this question tests your ability to evaluate the complexity of the code.
unfinished code:
def print_grid(grid: list) -> None: for row in grid: for cell in row: print('[' + cell + ']', end='') print()
def get_neighbours(grid: list, row: int, col: int) -> int: num_neighbours = 0 # above row if row > 0: # check above if col != 0: # check left above if grid[row-1][col-1] == '@': num_neighbours += 1 if grid[row-1][col] == '@': # check direcly above num_neighbours += 1 if col != len(grid[row])-1: # check right above if grid[row-1][col+1] == '@': num_neighbours += 1 if col != 0: # check left if grid[row][col-1] == '@': num_neighbours += 1 if col != len(grid[row])-1: # check right if grid[row][col+1] == '@': num_neighbours += 1 # below row if row < len(grid)-1: # check below if col != 0: # check left below if grid[row+1][col-1] == '@': num_neighbours += 1 if grid[row+1][col] == '@': # check direcly below num_neighbours += 1 if col != len(grid[row])-1: # check right below if grid[row+1][col+1] == '@': num_neighbours += 1 return num_neighbours
def calculate_next_grid(old_grid: list, num_rows: int, num_cols: int) -> list: new_grid = [] for row_index in range(num_rows): new_row = [] for col_index in range(num_cols): num_neighbours = get_neighbours(old_grid, row_index, col_index) current_cell = old_grid[row_index][col_index] if current_cell == '@': if num_neighbours == 2 or num_neighbours == 3: new_row.append('@') else: new_row.append(' ') else: if num_neighbours == 3: new_row.append('@') else: new_row.append(' ') new_grid.append(new_row) return new_grid
def make_grid(rows:int, cols:int) -> list: import random grid = [] for row in range(rows): new_row = [] for col in range(cols): chance = random.randint(0, 3) if chance == 1: new_row.append('@') else: new_row.append(' ') grid.append(new_row) return grid
def run_program(generations: int, num_rows: int, num_cols: int) -> None: grid = make_grid(num_rows, num_cols) current_generation = 0 while current_generation < generations: print('Generation', current_generation) print_grid(grid) grid = calculate_next_grid(grid, num_rows, num_cols) current_generation += 1
run_program(10, 5, 7)
Postconditions: count is the number of occurrences of substring within main string
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started