Answer the question
In order to leave comments, you need to log in
How to send emoticons for the minimum number of operations?
The guy decided to send N emoticons to his girlfriend.
After typing M emoticons, he thought that it would be faster to get N emoticons with a certain combination of the following operations:
1. Erase the last emoticon [operation - " D "]
2. Copy all emoticons [operation - " C "]
3. Paste emoticons from the clipboard [operation - " V "]
You need to help the guy cope with the task in the minimum number of operations.
Display the symbolic algorithm.
Example: N = 12; M=5;
Conclusion: DCVV
Explanation (5-1) = 4; 4 + 4 + 4 = 12;
Answer the question
In order to leave comments, you need to log in
Python code for calculating "on the forehead" (brute force) and identifying patterns:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from itertools import product
def process(existing, required):
for opcount in xrange(existing+required):
for combo in product('dcv', repeat=opcount):
buffer = 0
smiles = existing
data_y = [existing]
for char in combo:
if char == 'd':
smiles -= 1
elif char == 'v':
smiles += buffer
else:
buffer = smiles
data_y.append(smiles)
if smiles == required:
data_x = xrange(opcount+1)
plt.plot(data_x, data_y, linewidth=1)
print required, data_y
return ''.join(combo)
return '-'
for required in xrange(2, 101):
plt.clf()
plt.grid(True)
plt.title('Target: %s' % required)
plt.autoscale(enable=True, axis='both', tight=False)
for existing in xrange(1, (required//2)+1):
result = process(existing, required)
plt.savefig('{:02d}.png'.format(required), dpi=96, bbox_inches='tight')
ceil(12/5) = 3 -> Npastes=3-1=2
floor(12/3) = 4 -> copyOn = 4
12-4*2=4 -> Ndeletes = 5-4 = 1
Same with any other numbers. Let's say N=14, M=5:
ceil(14/5) = 3 -> Npastes = 2
floor(14/2) = 7 -> copyOn = 5 (since we can't catch up to seven)
14 - 5*2 = 4 -> Ndeletes = 1
Answer: CDVV
It's easy to figure out when to write DC and when to write CD.
Otherwise, in general, it turns out that I decided to do my homework for free!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question