Off Topic > Off Topic

Programming Megathread

Pages: << < (194/241) > >>

Bloody Mary:


--- Quote from: Timestamp on October 29, 2016, 12:34:58 PM ---i'm having a bit of a problem with this kata:


--- Code: ---In this kata you get the start number and the end number of a region and should return the count of all numbers except numbers with a 5 in it. The start and the end number are both inclusive!
Examples:
1,9 -> 1,2,3,4,6,7,8,9 -> Result 8
4,17 -> 4,6,7,8,9,10,11,12,13,14,16,17 -> Result 12
The result may contain fives. ;-)
The start number will always be smaller than the end number. Both numbers can be also negative!

--- End code ---

you could say i got some idea, but the execution is bad lol

--- End quote ---

Ah, this one is a pretty good practice problem. Let's not worry about the 5 rule for now and just devise a solution for finding the count. If we're given two numbers, we can turn them into a list with the range() command. Then we can iterate and count that.

Our prototype function will look like:

--- Code: ---def counter(start, end):
    nums = range(start, end+1)     #range() is exclusive; range(x,y) will yield a list of [x, x+1.....y-1]. we add 1 to the end to offset this
    count = 0
    for item in nums:
        count += 1
    return count

--- End code ---

This works pretty well for just counting, but what about the 5 rule? Well, there are a number of ways to implement it. The method that immediately jumps out to me is by using the 'in' keyword, which can check if a certain element is in a certain other element. For example, saying:

--- Code: ---if 'hello' in '7777helloaaaa':
    return True

--- End code ---

will return true. The catch? It can't check if an integer is in another integer. Is all hope lost? Not at all. Using the str(x) function, we can cast an integer to a string. An example of this:


--- Code: ---def check_if_in(number, check):
    if str(check) in str(number):
        return True
    return False

--- End code ---
The above function, given two numbers, will check if one's in the other and return true if so.

The sample functions above should hopefully help you to find a solution. By combining them and reworking the logic a bit, you can get your desired output. There are different (and probably more efficient) ways to solve this kata: don't feel like you absolutely need to implement it this way if you don't want to!

I've put my solution below in transparent text, so you can check out how I did it if you want:

def dont_count_fives(start, end):
    nums = range(start, end+1)
    iter = 0
    for element in nums:
        if not '5' in str(element):
            iter += 1
    return iter



Becquerel:

I just remembered that I made this thead about 366 Days ago
Happy Anniversary, thread!


SubDaWoofer:

am i the only person that abuses token's in python just to jar my way through any string/integer problems
topkek

Timestamp:

huge bump i want this topic to be alive because it's part of my soul


question, what do you guys think about lua? i'm thinking about learning lua a lot more probably as much as python

Steve5451²:


--- Quote from: Timestamp on November 05, 2016, 10:12:56 PM ---huge bump i want this topic to be alive because it's part of my soul


question, what do you guys think about lua? i'm thinking about learning lua a lot more probably as much as python

--- End quote ---

It's interpreted faster than Python but it also has a smaller community so you might not get as much help from Google as you would with Python.
EDIT: If you like Gmod making addons for it is really easy. Having never used it I was able to make some after a quick crashcourse on its syntax.

Pages: << < (194/241) > >>

Go to full version