#๐Ÿ”’ someone guide me in my coding solution

52 messages ยท Page 1 of 1 (latest)

lost mauve
#
def lucky_seven(n):
    '''
    Assume n is an integer. A number is considered lucky if it 
    has more 7 digits than 0 digits. How many lucky numbers are 
    there between 1-n inclusive?
    For example, 771203 is lucky, but 123 and 207 are not.
    '''
    seven_count = 0
    zero_count = 0
    else_count = 0
    for num in range(n):
        if num == 7:
            seven_count += 1
        elif num == 0:
            zero_count += 1
        else:
            else_count += 0
            
    
    if seven_count > zero_count:
        return(seven_count)
    else:
        return("0")
print(lucky_seven(771203))
alpine matrixBOT
#

@lost mauve

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lost mauve
#

im not 100% sure i know what the question is asking

raw scroll
# lost mauve im not 100% sure i know what the question is asking

Suppose you want to calculate lucky_seven(100). There are 100 numbers between 1 and 100 inclusive: 1, 2, 3, ..., 100.
Of these, it so happens that there are 18 numbers with more sevens than zeros: [7, 17, 27, 37, 47, 57, 67, 71, 72, 73, 74, 75, 76, 77, 78, 79, 87, 97].
So lucky_seven(100) is 18.

lost mauve
#

oh

#

i get it now

#

what are the steps to code

#

it seems hard to compare 7's with 0's for each number

raw scroll
#

It's easy if you convert the number to a string first.

#

(if I encountered this problem on a contest or something, I'd expect the inputs to be, like, 10000000 and hence one needs to write a solution that doesn't need to consider each number in order to calculate how many there'll be. But I don't know where you got this from, so.)

lost mauve
#

how can i fix the middle of the code

mortal wedge
# lost mauve

I think you might have meant to use if instead of for

#

you should be checking though if num_str == "7"

#

and the same for 0

#

I don't know why you're checking 6 though

#

actually that still wouldn't quite be right. Consider using str.count

#

!d str.count

alpine matrixBOT
#

str.count(sub[, start[, end]])```
Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation.

If *sub* is empty, returns the number of empty strings between characters which is the length of the string plus one.
lost mauve
#

i figured out how to do with with str.count

#

but i want to see if theres a way to do it with code

#

i dont think theres a easy way of doing it without using .count

languid willow
lost mauve
languid willow
lost mauve
# lost mauve

oh nvm i though this was considered using a for loop

languid willow
#

did you find a solution without using .count()?

lost mauve
#

nope

languid willow
#

you want to see one?

#

or just have hints?

lost mauve
#

better to see

languid willow
#

!e

n = 771204
seven = 0
zero = 0
for num in str(n):
    if num == "7":
        seven += 1
    elif num == "0":
        zero += 1

print(f"{seven = }")
print(f"{zero = }")
alpine matrixBOT
#

@languid willow :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | seven = 2
002 | zero = 1
languid willow
#

you run through all the characters of the number one by one and compare them with another string of one character and if it matches you increment that counter

lost mauve
#

hmm

#

i see

#

i was thinking something very similar to that

#

are you able to help with this

#
def color_mixer(s):
    '''
    Assume that s is a string containing only the characters 
    'r','g','b'. Create and return a new string whose characters 
    are a mix of adjacent colors as follows: 
    'rg' or 'gr' mixes to 'y' (yellow) 
    'gb' or 'bg' mixes to 'c' (cyan) 
    'rb' or 'br' mixes to 'm' (magenta) 
    If adjacent letters are the same, the color stays the same. 
    For example: 
    'rgb' should return 'yc'
    'bgrrgb' should return 'cyryc'
    '''
    string = " "
    for i in range(len(s)):
        if s[i:i+1]  == "rg" or s[i:i+1]  == "gr":
            string = string[1:] + "y"
            return string
        elif s[i:i+1]  == "gb" or s[i:i+1]  =="bg":
            string = string[1:] + "c"
            return string
        elif s[i:i+1]  == "rb" or s[i:i+1]  =="br":
            string = string[1:] + "m"
            return string
        else:
            string = string[1:]
            return string

print(color_mixer("bgrrgb"))
languid willow
#

when you send code here on discord you can put the two characters py just after the three backticks on the first line and then begin write the code on the next line

#

you can even edit this code up there to do that after the fact

lost mauve
#

changed

languid willow
#

have you seen this kind of syntax before?

if s[i:i+1] in ("rg", "gr"):
#

is will compare i data with any of the elements of that tuple

#

so it's kind of a or condition, but it's shorter, especially if you have many patterns to test against in the same if statement

#

put a print(s[i:i+1]) on a new line between the line with the for and the if and see what it is you are getting

languid willow
# lost mauve changed

after adding that print statement, look at what is being printed on each line, i'm guessing that is not what you are expecting

#

after that you might see what needs to be changed to get what you expect being printed on each of those lines

alpine matrixBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.