Last active
December 27, 2015 15:39
-
-
Save connorbode/7349522 to your computer and use it in GitHub Desktop.
SCS Interview Challenges : VAMPIRE NUMBERS o_O
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Problem: | |
A vampire number is a number v = x * y s.t. x and y are not both | |
divisible by 10 and the digits of v consit of the digits of x and | |
the digits of y. The number of digits n in the vampire number | |
must be even, and the number of digits in each of the two fangs | |
x and y must be half the number of digits in v. For instance, | |
1260 = 21 x 60 and 1395 = 15 x 93 are both vampire numbers. | |
Write a program that calculates the vampire numbers of n digits. | |
''' | |
def vamp(n): | |
# Check that input was a number | |
if(not isinstance(n, int)): | |
return "Input must be an integer" | |
# Create our limit | |
limit = int(n * "9") | |
# Vampire numbers | |
vamp = [] | |
# Begin computation | |
i=1 | |
while i < limit: | |
for j in range(i, limit): | |
# If the number is too big | |
if i*j > limit: | |
break | |
# If i & j aren't both divisble by 10.. | |
if not (i % 10 == 0 and j % 10 == 0): | |
# Make copies of the numbers as strings | |
s1 = str(i) + str(j) | |
s2 = str(i*j) | |
# Check if the digits match | |
if match(s1, s2): | |
print str(i) + " * " + str(j) + " = " + str(i * j) | |
vamp.append(i * j) | |
i = i + 1 | |
# Print the vampire numbers | |
print "" | |
print "VAMPIRE NUMBERS WITH " + str(n) + " DIGITS:" | |
print "" | |
vamp.sort() | |
for v in vamp: | |
print v | |
print ' .-"""".' | |
print " / \\" | |
print " __ / .-. .\\" | |
print " / `\ / \/ \\" | |
print " | _ \/ .==.==." | |
print " | ( \ /____\__\\" | |
print " \ \ (_()(_()" | |
print " \ \ '---._" | |
print " \ \_" | |
print " /\ |` (__)________/" | |
print " / \| /\___/" | |
print " | \ \||VV" | |
print ' | \ \|"""",' | |
print " | \ ______)" | |
print " \ \ /`" | |
print " BOOM! \(" | |
# Checks if two integers contain the same digits (possibly rearranged) | |
def match(i, j): | |
# Iterate over the two strings | |
match = 1 | |
for c in i: | |
char_found = 0 | |
k = 0 | |
while k < len(j): | |
# If we find a match, continue | |
if c == j[k]: | |
# note we found a match | |
char_found = 1 | |
# delete the character | |
j = j.replace(c, "", 1) | |
break | |
k = k + 1 | |
# If we didn't find the character, it's not a match | |
if char_found == 0: | |
match = 0 | |
return match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment