Post
avtar img holder

Yonseo

2019-08-05 12:15:24
  • Last Edit: 0000-00-00 00:00:00
  • HackerRank Python Challenge

    avtar img holder

    Given a range of numbers x and y, how many non repeating numbers are there?

    For example: range 219, 220, 221, 222, 223

    220, 221, 222, 223 are matching numbers because digit 2 appears twice in each number. 

    219 is a non matching number. There is no repeating digit.

    Think before you Code

    There are 3 problems. First is taking two numbers and creating a range. Lucky for me there's a function for that range(x, y). The next problem is taking each number in the array and converting it into an array because I need to filter out each digit to find a repeating number. In a the array, if it appears more than once it is a match. Finally the last problem is creating an array of non matching numbers and counting that array to return the result.

    Code:

    def countNumbers(array):

    y = range(array[0], array[1] + 1)
    if len(y) > 1:
    count_item = 0
    match_array = []
    no_match_array = []
    match = False

    for item in y:
    single_digit_array = [int(d) for d in str(item)]
    count_digit = 0
    for i in single_digit_array:
    single = single_digit_array[count_digit]
    z = single_digit_array.count(single)
    if z > 1 :
    match = True
    else:
    match = False
    count_digit = count_digit + 1
    if match == True:
    match_array.append(item)
    else:
    no_match_array.append(item)

    count_item = count_item + 1
    result = len(no_match_array)
    else:
    result = None

    print 'There are: ' + str(result) + ' non matching numbers'
    print no_match_array
    print match_array

    x = 219
    y = 223
    array = [x, y]
    countNumbers(array)


    Result:

    There are: 1 non matching numbers

    [219]

    [220, 221, 222, 223]

    Improvements:

    I can improve my code by breaking the loop when a match is found so a number like 0012334 will not have to check for a repeating digit if one is already found.


    Latest Posts

    avtar img holder
    First time using Unreal Game Engine
    Category
    avtar img holder
    React JS Study Notes
    Category
    avtar img holder
    Creating a Component in React JS
    Category
    avtar img holder
    Javascript for Bubble Tea
    Category
    avtar img holder
    Github Commands
    Category
    avtar img holder
    FuelPHP Login & Authentication
    Category
    avtar img holder
    Intro to C++
    Category

    COPYRIGHT © 2019Taiyaki.io All rights ReservedHand-crafted & Made with