Question

In: Computer Science

Question: Reverse engineering (Python if possible) In the FizzBuzz question, it's an exercise to calculate a...

Question: Reverse engineering (Python if possible)

In the FizzBuzz question, it's an exercise to calculate a value based on whether the input was divisible by 3, 5, or both. If we looped that function through integers from one, the first 10 return values would be:

None # For input 1
None # For input 2
'3'
None
'5'
'3'
None
None
'3'
'5'

In this exercise, we'll try to reverse engineer the output of a similar process.

The sequence of numbers is generated as follows. Instead of checking division by 3 and 5, we have designated, say, three numbers 2, 4, 5. Whenever the input is divisible by 2, our output will include the letter a, when by 4, the letter b, when by 5, the letter c. Otherwise there is no output. So the generated sequence from one onwards would be

None # for input 1
'a' # for input 2
None # for input 3
'ab' # for input 4
'c'
'a'
None
'ab'
None
'ac' # for input 10

To make things interesting, we will not include the None values, giving us the sequence

'a'
'ab'
'c'
'a'
'ab'
'ac'

This is the input for the exercise. Your task is to "reverse engineer" a, b, c from the sequence. That is, you'll infer what integer values each letter corresponds to. There are multiple possible solutions: your solution should pick the lowest possible values.

Here's another example:

'a'
'b'
'a'
'a'
'b'
'a'

The solution is a = 3, b = 5. (You may confirm these are the lowest possible values that match the sequence.)

Complete the function reverse_engineer. Note that the test cases may have up to twenty different letters to reverse engineer and up to four-digit integers as solutions.

Solutions

Expert Solution

Code screenshot:

reverse_engg.py

def rev_engg(divisors,n):
   # List that stores result
   res = []
   # From integers 1 to n
   for i in range(1,n+1):
       # Initilize an empty string
       s = ''
       # Check for each divisor
       for j in range(len(divisors)):
           # If i is divisible
           if i%divisors[j]==0:
               # Then append corresponding character
               s += chr(97+j)
       # If string is not empty then append s to res
       if s!='':
           res.append(s)
   # Return res
   return res
# Function calling
print(rev_engg([3,5],10))
print(rev_engg([2,4,5],10))

output screenshot:


Related Solutions

What is the relationship between law, ethics, and reverse engineering? Why is reverse engineering a subject...
What is the relationship between law, ethics, and reverse engineering? Why is reverse engineering a subject for regulation or ethical questions?
Vibrations Question in Mechanical Engineering: Vibrating System Stability Question Describe in as much detail as possible...
Vibrations Question in Mechanical Engineering: Vibrating System Stability Question Describe in as much detail as possible what is meant by a stable vibrating system and state the condition which must be satisfied to achieve stability.
In python! please be thorough and read question carefully :) if possible, give explanation for code...
In python! please be thorough and read question carefully :) if possible, give explanation for code We’re going to create a class which stores information about product ratings on a website such as Amazon. The ratings are all numbers between 1 and 5 and represent a number of “stars” that the customer gives to the product. We will store this information as a list of integers which is maintained inside the class StarRatings. You will be asked to write the...
Is reverse engineering viewed as a legitimate or illegal act through the lens of the law?
Is reverse engineering viewed as a legitimate or illegal act through the lens of the law?
Debate the ethical issue(s) surrounding reverse engineering in the context of software.
Debate the ethical issue(s) surrounding reverse engineering in the context of software.
Redesigning a database involves the process of reverse engineering a legacy database to create a data...
Redesigning a database involves the process of reverse engineering a legacy database to create a data model. There are a host of software tools available to assist with this process. Identify 2 separate tools that will reverse engineer an existing database. What database management systems (Microsoft SQL, Oracle, MySQL, DB2…) will this system work with (list all that apply)? Briefly describe how the tools work for reverse engineering an existing database and the output that the tool provides.
AEROSPACE ENGINEERING QUESTION 1) Calculate the synodic period of Venus with respect to Earth. I'm not...
AEROSPACE ENGINEERING QUESTION 1) Calculate the synodic period of Venus with respect to Earth. I'm not sure exactly how to do this. Our professor hasn't given us a formula or anything. Can someone please give me a hand. Thank You. EDIT: I FIGURED IT OUT. :)
define ADHD and describe it's symptoms and possible treatment options?
define ADHD and describe it's symptoms and possible treatment options?
It's possible to estimate the percentage of fat in the body by measuring the resistance of...
It's possible to estimate the percentage of fat in the body by measuring the resistance of the upper leg rather than the upper arm; the calculation is similar. A person's leg meas-ures 40cm between the knee and the hip, with an average leg diameter (ignoring bone and other poorly conducting tissue) of 12cm . A potential difference of 0.79V causes a current of 1.6mA . What are the fractions of muscle and fat in the leg? Please show full work,...
Please perform reverse engineering to compose a UML class diagram for the provided java code /*...
Please perform reverse engineering to compose a UML class diagram for the provided java code /* Encapsulated family of Algorithms * Interface and its implementations */ public interface IBrakeBehavior { public void brake(); } public class BrakeWithABS implements IBrakeBehavior { public void brake() { System.out.println("Brake with ABS applied"); } } public class Brake implements IBrakeBehavior { public void brake() { System.out.println("Simple Brake applied"); } } /* Client that can use the algorithms above interchangeably */ public abstract class Car {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT