Skip to content

Latest commit

 

History

History
85 lines (61 loc) · 3.74 KB

File metadata and controls

85 lines (61 loc) · 3.74 KB

For this project, you will need to understand several key algorithmic and mathematical concepts to devise a solution that efficiently calculates the minimum number of operations to achieve a given number of characters using only “Copy All” and “Paste” operations. Here is a list of concepts and resources that will be helpful:

Concepts Needed:

  1. Dynamic Programming:

  2. Prime Factorization:

    • Understanding how to perform prime factorization is crucial since the problem can be reduced to finding the sum of the prime factors of the target number n.
    • Prime Factorization (Khan Academy)
  3. Code Optimization:

    • Knowing how to approach problems from an optimization perspective can be useful in finding the most efficient solution.
    • How to optimize Python code
  4. Greedy Algorithms:

  5. Basic Python Programming:

By studying these concepts and utilizing the resources provided, you will be equipped to tackle the “Minimum Operations” problem effectively, applying both mathematical reasoning and programming skills to find the most efficient solution.

Additional Resources

Requirements

General

  • Allowed editors: vi, vim, emacs
  • All your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.4.3)
  • All your files should end with a new line
  • The first line of all your files should be exactly #!/usr/bin/python3
  • A README.md file, at the root of the folder of the project, is mandatory
  • Your code should be documented
  • Your code should use the PEP 8 style (version 1.7.x)
  • All your files must be executable

Tasks

0. Minimum Operations

In a text file, there is a single character H. Your text editor can execute only two operations in this file: Copy All and Paste. Given a number n, write a method that calculates the fewest number of operations needed to result in exactly n H characters in the file.

  • Prototype: def minOperations(n)
  • Returns an integer
  • If n is impossible to achieve, return 0

Example:

n = 9

H => Copy All => Paste => HH => Paste => HHH => Copy All => Paste => HHHHHH => Paste => HHHHHHHHH

Number of operations: 6

carrie@ubuntu:~/0x02-minoperations$ cat 0-main.py
#!/usr/bin/python3
"""
Main file for testing
"""

minOperations = __import__('0-minoperations').minOperations

n = 4
print("Min # of operations to reach {} char: {}".format(n, minOperations(n)))

n = 12
print("Min # of operations to reach {} char: {}".format(n, minOperations(n)))

carrie@ubuntu:~/0x02-minoperations$
carrie@ubuntu:~/0x02-minoperations$ ./0-main.py
Min number of operations to reach 4 characters: 4
Min number of operations to reach 12 characters: 7
carrie@ubuntu:~/0x02-minoperations$

Repo:

  • GitHub repository: alx-interview
  • Directory: 0x02-minimum_operations
  • File: 0-minoperations.py