Challenges

Reddit challenges in python language.

Files Code and Result

challenge1_easy

challenge2_easy

challenge3_easy

challenge4_easy

challenge5_easy

challenge6_easy

challenge7_easy

challenge8_easy

challenge9_easy

challenge10_easy

challenge11.easy

challenge12_easy

challenge13_easy

challenge14_easy

challenge15_easy

challenge16_easy

challenge17_easy

challenge18_easy

challenge19_easy

challenge20_easy

challenge21_easy

challenge22_easy

challenge23_easy

challenge25_easy

challenge26_easy

challenge27_easy

challenge28_easy

challenge29_easy

challenge30_easy

challenge31_easy

challenge32_easy

challenge33_easy

challenge34_easy

challenge35_easy

challenge36_easy

challenge37_easy

challenge38_easy

challenge39_easy

challenge40_easy

challenge41_easy

challenge42_easy

challenge44_easy

challenge45_easy

challenge46_easy

challenge47_easy

challenge48_easy

challenge49_easy

challenge50_easy

challenge51_easy

challenge52_easy

challenge53_easy

challenge54_easy

challenge55_easy

challenge56_easy

challenge57_easy

challenge58_easy

challenge59_easy

challenge60_easy

challenge61_easy

challenge62_easy

challenge63_easy

challenge64_easy

challenge65_easy

challenge66_Dev

challenge66_easy

challenge67_easy

challenge68_easy

challenge69_easy

challenge69_easyBonus

challenge70_easy

challenge71_easy.py

challenge72_easy

challenge73_easy

challenge74_easy

challenge76_easy

challenge77_easy

challenge79_easy

challenge80_easyone

challenge82_easy

challenge83_easy

challenge84_easy

challenge85_easy

challenge85_easybonus

challenge86_easy

challenge87_easy

challenge88_easy

challenge88_easybonus

challenge89_easy

challenge91_easy

challenge92_easy

challenge93_easy

challenge94_easy

challenge95_easy

challenge96_easy

challenge97_easy

challenge98_easy

challenge99_easy

challenge100_easy

challenge100_easybonus

challenge101_easy

challenge101_easybonus

challenge102_easy

challenge103_easy

challenge104_easy

challenge105_easy

challenge106_easy

challenge107_easy

challenge108_easy

challenge109_easy

challenge110_easy

challenge111_easy2

challenge112_easy

challenge113_easy

challenge114_easy

challenge115_easy

challenge116_easy

challenge117_easy

challenge118_easy

challenge119_easy

challenge121_easy

challenge122_easy

challenge124_easy

challenge125_easy

challenge126_easy

challenge127_easy

challenge128_easy

challenge130_easy

challenge131_east

challenge132_easy

challenge133_easy

challenge134_easy

challenge135_easy

challenge136_easy

challenge137_easy

challenge138_easy

challenge139_easy

challenge140_easy

challenge141_easy

challenge143_easy

challenge146_easy

challenge148_easy

challenge149_easy

challenge153_easy

challenge154_easy

challenge158_easy

challenge158_easybonus

challenge159_easy

challenge160_easy

challenge169_easy

challenge169_easybonus

challenge172_easy

challenge174_easy

challenge175_easy

challenge177_easy

challenge180_easy

challenge181_easy

challenge185_easy

challenge188_easy

challenge192_easy

challenge193_easy

challenge197_easy

challenge198_easy

challenge199_easy

challenge201_easy

challenge202_easy

challenge203_easy

challenge204_easy

challenge204_easyBonus

challenge205_easy

challenge205_easyBonu

challenge206_easy

challenge208_easy

challenge210_easy

challenge211_easy

challenge212_easy

challenge212_easybonus

challenge213_easy

challenge214_easy

challenge215_easy

challenge216_easy

challenge217_easy

challenge218_easy

challenge218_easyBonus1

challenge218_easyBunus2

challenge219_easy

challenge220_easy.py

challenge221_easy

challenge221_easy

challenge222_easy

challenge222_easy.py

challenge223_easy

challenge226_easy

challenge226_easy

challenge228_easy.py

challenge228_easydev

challenge229_easy

challenge229_easybonus.py

challenge232_easy

challenge232_easy

challenge232_easybonus

challenge232_easybonus

challenge234_easy

challenge234_easybonus

challenge235_easy

challenge236_easy

challenge237_easy

challenge238_easy

challenge238_easybonus

challenge239_easy

challenge240_easy

challenge242_easy

challenge242_easy

challenge242_easyBonus

challenge243_easy

challenge245_easy

challenge245_easyBonus

challenge246_easy

challenge247_easy

challenge249_easy

challenge252_easy

challenge254_easy

challenge255_easy

challenge270_easy

challenge284_easy

challenge286_easy

challenge287_easy

challenge288_easy

challenge290_easy

challenge290_easybonus

challenge291_easy

challenge291_easybonus

challenge83_easy

'''
One of the most annoying and confusing differences between English and basically every other language
in the world is that English uses a weird way to name very large numbers.

For instance, if you wanted to translate "one trillion" from English to French, you might think it
would be "un trillion", but that is wrong. The correct translation of "one trillion" to French is
"un billion". Well, then, you might ask, what is "one billion" in French? It is, in fact, "un milliard".
And "un trillion" in French is equal to english "one quintillion". Confusing, no?

The reason for this is that there are two so-called scales for large numbers, the long scale and the
short scale. English uses the short scale, almost everyone else uses the long scale (though the Arabic
languages also apparently use the short scale). The two systems can be summarized as follows:

    In the short scale, you get a "new word" for the numbers every time the number increases by a factor
    of 1,000. So "a thousand millions" is "one billion" and "a thousand billions" is "one trillion".

    In the long scale, you get a "new word" for the numbers every time the number increases by a factor
    of 1,000,000. So "a million millions" is the same as "one billion" and "a million billions" is the
    same as "one trillion". If we just increase by a factor of 1,000, the "-on" ending on the word is
    replaced by "-ard". So "a thousand millions" is "one milliard", "a thousand billions" is "one billiard".

Here's a table summarizing the words for different numbers:
Actual number 	Short scale name 	Long scale name
106 	million 	million
109 	billion 	milliard
1012 	trillion 	billion
1015 	quadrillion 	billiard
1018 	quintillion 	trillion
1021 	sextillion 	trilliard

And it goes on like that.

Your task today is to write a program that will print out the name of a number in both long-scale and
short-scale. So, for instance, given 1234567891111, it should print out

Short scale: 1 trillion, 234 billion, 567 million, 891 thousand and 111
Long scale:  1 billion, 234 milliard, 567 million, 891 thousand and 111

Bonus points if it prints out everything in letters, i.e.:

Short scale: one trillion, two hundred and thirty-four billion, five hundred
and sixty-seven million, eight hundred and ninety-one thousand and one
hundred and eleven

Long scale:  one billion, two hundred and thirty-four milliard, five hundred
and sixty-seven million, eight hundred and ninety-one thousand and one
hundred and eleven

The program should be able to handle all numbers that can fit into an unsigned 64-bit integers,
i.e. all numbers up to 264 - 1 ("18 trillion, 446 billiard, 744 billion, 73 milliard, 709 million,
551 thousand and 615" in long scale, though it's something completely different in short scale.),
or 263 - 1 if you're using signed 64-bit integers. Of course, you can write your program so it can
handle bigger numbers if you want, but it's not necessary.

'''

ones = ["one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
        "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
        "eighteen", "nineteen"]
tens = ["twenty", "thirty", "forty", "fifty",
        "sixty", "seventy", "eighty", "ninety"]
hundred = 'hundred'
english = [" thousand ", "million ", "billion ",
        "trillion", "quadrillion,", "quintillion,", "sextillion,"]
french = ["thousand,", "million,", "milliard,",
        "billion,", "billiard,", "trillion,", "trilliard,"]
plus = 'and'

def decode_short(num):
    ones_slice = num[-2:]
    tens_slice = num[-2:-1]
    hundred_slice = num[-3:]
    thou_slice = num[-6:-3]
    mill_slice = num[-9:-6]
    bill_slice = num[-12:-9]
    trillion_slice = num[-15:-12]
    string_num = ''
    flag = 0
    if trillion_slice == '':
        pass
    else:
        trillion_slice = trillion_slice.zfill(3)
        trillion_slice = substring(bill_slice, flag, string_num)
        string_num += ' ' + trillion_slice + ' trillion'
        flag = 1
    if bill_slice == '':
        pass
    else:
        bill_slice = bill_slice.zfill(3)
        bill_slice = substring(bill_slice, flag, string_num)
        string_num += ' ' + bill_slice + ' billion'
        flag = 1

    if mill_slice == ''or mill_slice == '000':
        pass
    else:
        mill_slice = mill_slice.zfill(3)
        mill_slice = substring(mill_slice, flag, string_num)
        string_num += ' ' + mill_slice + ' million'
        flag = 1

    if thou_slice == '' or thou_slice == '000':
        pass
    else:
        thou_slice = thou_slice.zfill(3)
        thou_slice = substring(thou_slice, flag, string_num)
        thou = tens_slice
        eng = english[0]
        string_num += ' ' + thou_slice + english[0]
        flag = 1

    if hundred_slice == '' or hundred_slice == '0':
        if string_num == '':
            pass
        else:
            string_num += ' and'

    else:
        hundred_slice = hundred_slice.zfill(3)
        hundred_slice = substring(hundred_slice, flag, string_num)
        string_num += hundred_slice
        flag = 1
    return string_num


def substring(n, flag, stringnum):
    ''' str, bool, str
    deal with numbers 1 to 999'''
    strng = ''
    # slice for multiple of tens
    try:
        int_n = int(n[1:3])
    except:
        int_n = 0
    # slice for multiples of hundreds
    hund = int(n[0])
    if n[0] == '0' and stringnum != '':
        strng += 'and '
    elif hund > 0:
        strng += ' ' + ones[hund - 1] + ' hundred and '

    if int_n <= 19:
        strng += ones[int_n - 1]
    else:
        str_n = str(int_n)
        dec = int(str_n[0])
        strng += tens[dec - 2]
        digit = int(str_n[1])
        # pass if a zero
        if digit == 0:
            pass
        # add the final digit
        else:
            strng += ' ' + ones[digit - 1]
    return strng


if __name__ == '__main__':
    num = 110
    num = str(num)
    ans = decode_short(num)
    print(ans)

Result

 one hundred and ten