Skip to content

Instantly share code, notes, and snippets.

@Madrigal
Created November 16, 2013 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Madrigal/7506841 to your computer and use it in GitHub Desktop.
Save Madrigal/7506841 to your computer and use it in GitHub Desktop.
Script to solve the problem described in http://orsaibonsai.com/blog/post/gran_7 XX/XXX + XX/XX = 7 With numbers from 1 to 9, fill all the slots without repeating numbers to sum 7
#! /usr/bin/env python
# // XX/YYY + XX/YY = 7
import random
numbers = [1,2,3,4,5,6,7,8,9]
res = 0
target = 7
def pop_random (num):
res = []
for i in range(0,num):
# Select a random number and delete it
value = random.sample(numbers, 1)
numbers.pop(numbers.index(value[0]))
res.append(value[0])
return res
def list_to_num(list):
base = 10
res = 0
for i in list:
res *= base
res += i
return res
def equal(num1, num2):
# As we are dealing with floats, define an equal function
return abs(num1 - num2) < 0.000001
def test_solution(a, b, c, d):
temp1 = float(a)/float(b)
temp2 = float(c)/float(d)
res = float(temp1) + float(temp2)
return float(res)
while not equal(res, target):
res1 = pop_random(2)
res1 = list_to_num(res1)
res2 = pop_random(3)
res2 = list_to_num(res2)
res3 = pop_random(2)
res3 = list_to_num(res3)
res4 = pop_random(2)
res4 = list_to_num(res4)
res = test_solution(res1, res2, res3, res4)
print res
# Re-populate numbers
numbers = [1,2,3,4,5,6,7,8,9]
print res1, res2, res3, res4
@apoz
Copy link

apoz commented Nov 17, 2013

Se puede hacer mucho más fácil, no? :P

https://gist.github.com/apoz/7507165

@Madrigal
Copy link
Author

Madrigal commented Oct 1, 2014

Hasta apenas hoy lo voy viendo, definitivamente mucho más fácil :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment