Question

Random characters You decide to create a program characters.py that fills a two-dimensional list with random...

Random characters You decide to create a program characters.py that fills a two-dimensional list with random symbols, and then performs some operations on it. To create a grid with 5 rows and 4 columns, you include this line in your main function: grid = [[0]*4 for i in range(5)] You then write four helper functions: fill_grid(g) fills grid with random symbols print_grid(g) prints grid count(g, s) counts how many times a symbol occurs replace(g, s1, s2) replaces one symbol with another Your main function asks the user for the symbol to count and the one to replace it with, and displays the grid at each step. A sample run might look like this:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import random #random package

def replace(g,s1,s2): #replace function
   for i in range(len(g)): #iterates length of grid times
       for j in range(len(g[i])): #iterates through each column
           if (g[i][j]==s1): #if symbol is found
               g[i][j]=s2 #replaces the old symbol with new symbol
   return g #returns grid

def count(g,s): #count function
   count=0 #count
   for i in g: #iterates through each row
       for j in i: #iterates through each column
           if (j==s): #if symbol is found
               count+=1 #count increases
   print("Count of symbol",s,"is",count) #prints count

def fill_grid(g): #fill_grid function
   g = [[0]*4 for i in range(5)]
   for i in range(0,len(g)):
       for j in range(4):
           g[i][j]=chr(random.randrange(33,47)) #fills the grid with char( random number )
   return g

def print_grid(g):
   for i in g:
       for j in i:
           print(j," ",end="")
       print("\n")
      
def main():
   g=[]
   g=fill_grid(g)
   print_grid(g)
  
   s=input("Enter symbol to count")
   count(g,s)
   print_grid(g)
  
   s1=s
   s2=input("Enter new symbol")
   g=replace(g,s1,s2)
   print_grid(g)

if __name__=="__main__":
   main()
  

Add a comment
Know the answer?
Add Answer to:
Random characters You decide to create a program characters.py that fills a two-dimensional list with random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code to create a two-dimensional array named numbers containing 500 random numbers from 1 to...

    Write code to create a two-dimensional array named numbers containing 500 random numbers from 1 to 1000 inclusive. The array has 50 rows. Then walk through the array and output the random numbers 10 to a line. Format the numbers in right-aligned columns. Use the command prompt window for output. Note: do NOT create a class or main.

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Write a program that, given a starting location in a 2D grid of characters, will count...

    Write a program that, given a starting location in a 2D grid of characters, will count how many contiguously connected @ symbols there are. Cells have to be direct neighbors either horizontally or vertically to count as contiguous. However, the right/left and top/bottom edges ARE considered to be connected – you can “wrap around” from one side of the matrix to the opposite one. The grid of chars will always be 10x10. Your program should load the grid of chars...

  • C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

    C++ Project - Create a memory game in c++ using structs and pointers. For this exercise, you will create a simple version of the Memory Game. You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • Having trouble with python! Write a program to build a list of grades, print them out,...

    Having trouble with python! Write a program to build a list of grades, print them out, add to them and count occurrences, and find the amount of the 2-digit grade values. Here are the criteria a) Your program “L6QI initials.py" must start with a commented ID Box AND include a comment that indicates the e of the program. EACH of the five functions in your program must state its purpose in comments too The main function in your program must...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT