Discuss Scratch

mybearworld
Scratcher
1000+ posts

Programming challanges!

gosoccerboy5 wrote:

Incomplete code:
function parseRN(numeral) {
  numeral = numeral.toUpperCase();
  let result = 0;
  let keys = {
    "I": 1, "V": 5, "X": 10,
    "L": 50, "C": 100, 
    "D": 500, "M": 1000,
  };
  for (i in numeral) {
    if (keys[numeral[i]] === undefined) {
      throw "Invalid Roman numeral";
    }
    result += keys[numeral[i]];
  }
  return result;
}
console.log(parseRN("MDCCCLXVII"));
It'll only get harder from here..
Would something like “IV” work like that?

Signatures are the only place where assets links still work.
gosoccerboy5
Scratcher
1000+ posts

Programming challanges!

mybearworld wrote:

Would something like “IV” work like that?
No, hence the “incomplete”. I am working on it

ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

mybearworld wrote:

Maybe generating Fibonacci numbers?

def fibonacci(num):
    list = [1, 1]
    i = 2
    while i < num:
        list.append(list[i-1] + list[i-2])
        i += 1
    return list
print(fibonacci(int(input("How Many? | "))))

how about detecting if a number is divisible by another?





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

ScratchCatHELLO wrote:

how about detecting if a number is divisible by another?

actually that's probably not that hard
def divisible(number, divider):
    return int(number)%int(divider)==0
while True:
    print(divisible(input('number to divide | '), input('divisor | ')))
    print('----------------------------------')

how about checking if a number is prime?





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
gosoccerboy5
Scratcher
1000+ posts

Programming challanges!

ScratchCatHELLO wrote:

how about checking if a number is prime?
I mean technically you could use brute force, but..

ScratchCatHELLO wrote:

how about detecting if a number is divisible by another?
That's extremely difficult, if only there were a built in language construct for that?

Last edited by gosoccerboy5 (April 21, 2021 20:04:00)


ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

gosoccerboy5 wrote:

ScratchCatHELLO wrote:

how about checking if a number is prime?
I mean technically you could use brute force, but..

ScratchCatHELLO wrote:

how about detecting if a number is divisible by another?
That's extremely difficult, if only there were a built in language construct for that?

sorry





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
mybearworld
Scratcher
1000+ posts

Programming challanges!

gosoccerboy5 wrote:

ScratchCatHELLO wrote:

how about detecting if a number is divisible by another?
That's extremely difficult, if only there were a built in language construct for that?
Sigh, if you could only do
x%y == 0


ScratchCatHELLO wrote:

how about checking if a number is prime?
Alright, that's not going to be too hard.
def is_prime(x, divider=False):
    for i in range(int(x)-1, 1, -1):
        if x%i == 0:
            return (False, i) if divider else False
    return (True, 1) if divider else True
while True:
    y = input("Please enter a number:")
    if y=="stop": break
    try:
        y = int(y)
    except:
        print("Please enter a whole number.\n")
        continue
    print(f"{y} is{'' if is_prime(y) else '''n't'''} prime\n")
When you do something like is_prime(25, divider=True) it will tell you (False, 5) as it isn't prime, and it is divisible by 5.
is_prime(3, divider=True) will give you (True, 1) as it is prime, so only divisible by one or itself.

Last edited by mybearworld (April 22, 2021 11:34:58)


Signatures are the only place where assets links still work.
ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

I'm still trying to figure out how to do roman numerals, but meanwhile I was trying to do other practice things and at some point I realized one of the challanges was to write the function that validates your function that validates your function that validates your function that validates your function


(read bottom left)





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
mybearworld
Scratcher
1000+ posts

Programming challanges!

ScratchCatHELLO wrote:

I'm still trying to figure out how to do roman numerals, but meanwhile I was trying to do other practice things and at some point I realized one of the challanges was to write the function that validates your function that validates your function that validates your function that validates your function


(read bottom left)
“The code must contain () for the parameter list
The code must not return ()”

Eh…

Signatures are the only place where assets links still work.
ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

mybearworld wrote:

ScratchCatHELLO wrote:

I'm still trying to figure out how to do roman numerals, but meanwhile I was trying to do other practice things and at some point I realized one of the challanges was to write the function that validates your function that validates your function that validates your function that validates your function


(read bottom left)
“The code must contain () for the parameter list
The code must not return ()”

Eh…

you can do ‘(’ + ‘)’ to avoid that, but yeah.





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
mybearworld
Scratcher
1000+ posts

Programming challanges!

Oooh, I understand what it wants from me. Alright, that's simple…
def validate(code):
    if not "def" in code:
        return "missing def"
    if not ":" in code:
        return "missing :"
    if chr(40)+chr(41) in code:
        return "missing param"
    if not (")" in code and "(" in code):
        return "missing paren"
    if not ("\t" in code or " "*4 in code):
        return "missing intend"
    if not "return" in code:
        return "missing return"
    if not "validate" in code:
        return "wrong name"
    return True
chr(40)+chr(41), because I'm not allowed to put () in my code

Signatures are the only place where assets links still work.
ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

mybearworld wrote:

Oooh, I understand what it wants from me. Alright, that's simple…
def validate(code):
    if not "def" in code:
        return "missing def"
    if not ":" in code:
        return "missing :"
    if chr(40)+chr(41) in code:
        return "missing param"
    if not (")" in code and "(" in code):
        return "missing paren"
    if not ("\t" in code or " "*4 in code):
        return "missing intend"
    if not "return" in code:
        return "missing return"
    if not "validate" in code:
        return "wrong name"
    return True
chr(40)+chr(41), because I'm not allowed to put () in my code

yeah, that was basically what their solution was
I did it using loops instead, which isn't very good for this but might work better if there were more criteria

required = {
    'def': 'missing def',
    ':': 'missing :',
    '(': 'missing paren',
    ')': 'missing paren',
    '    ': 'missing indent',
    '(' + ')': 'missing param',
    'validate': 'wrong name',
    'return': 'missing return'
}
def validate(code):
    for item in required:
        if item == '(' + ')':
            if item in code:
                return "missing param"
        else:
            if not item in code:
                return required[item]
    return True





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
mybearworld
Scratcher
1000+ posts

Programming challanges!

class Roman:
    romanConvert = {"I": 1,
                    "V": 5,
                    "X": 10,
                    "L": 50,
                    "C": 100,
                    "D": 500,
                    "M": 1000}
    romanLetters = romanConvert.keys()
    def __init__(self, x):
        self.number = str(x).upper()
    def __int__(self):
        x = -1
        result = 0
        while True:
            x += 1
            if x != len(self.number)-1 and self.romanConvert[self.number[x+1]] > self.romanConvert[self.number[x]]:
                result += self.romanConvert[self.number[x+1]]-self.romanConvert[self.number[x]]
                x += 1
            else:
                result += self.romanConvert[self.number[x-1]]
            if x == len(self.number)-1:
                return result
# I finally did it!
print(int(Roman("XXIV")))
Yay! Roman Numerals!

Last edited by mybearworld (June 3, 2021 20:51:20)


Signatures are the only place where assets links still work.
gosoccerboy5
Scratcher
1000+ posts

Programming challanges!

mybearworld wrote:

Yay! Roman Numerals!
I hate to burst your bubble, but I think XXIV means 24.. your program says 19..

mybearworld
Scratcher
1000+ posts

Programming challanges!

gosoccerboy5 wrote:

[View post]

mybearworld wrote:

Yay! Roman Numerals!
I hate to burst your bubble, but I think XXIV means 24.. your program says 19..
Oooops.

Last edited by mybearworld (Sept. 24, 2022 14:16:18)


Signatures are the only place where assets links still work.
mybearworld
Scratcher
1000+ posts

Programming challanges!

Bump after >6 months.

Get an input from 0-4. Undefined behavior for anything different. For each number, raise a different error.

Rules:
  • Do not use functions made to error, like Python's raise
  • If your programming language gives more information then just the error, than just the error counts. For example, in Python again, errors in line 1 and 4 won't be counted different, and “unsupported operand type(s) for +: ‘int’ and ‘tuple’” and “can only concatenate list (not ”dict“) to list” aren't different errors, because they're both “TypeError”s
  • Please say which errors are caused, and (optionally, but would be great), why.

Example (87 bytes, Python 3):
x=int(input())
if x==0:1/0
if x==1:x()
if x==2:f"{a}"
if x==3:eval("def")
x.x
  • 0 returns a ZeroDivisionError, no explanation needed
  • 1 returns a TypeError, because you can't run an integer. x is set as an integer, as it is what the user's answer is stored in. 1() would work just as well, but it also gives a SyntaxWarning.
  • 2 returns a NameError. I couldn't just use a simple “a”, because Python would complain. By putting it in an f-string, it only evaluates it when it needs to.
  • 3 returns a SyntaxError. This couldn't be put into an f-string, because you can't use defs in f-strings. Eval will still allow this.
  • 4 (and any other number, but that's undefined behavior) returns an ArgumentError, because an integer does not have the argument x.

Have fun!

Last edited by mybearworld (Feb. 19, 2022 11:26:10)


Signatures are the only place where assets links still work.
ScratchCatHELLO
Scratcher
1000+ posts

Programming challanges!

What if you don’t define anything for 4?





ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust



Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
mybearworld
Scratcher
1000+ posts

Programming challanges!

ScratchCatHELLO wrote:

What if you don’t define anything for 4?
You need to error for 4 too

Signatures are the only place where assets links still work.
applejuiceproduc
Scratcher
1000+ posts

Programming challanges!

mybearworld wrote:

gosoccerboy5 wrote:

Um.. any challenges?
How about… idk, a text reserver? With as few characters possible?
r=lambda x:x[::-1]
That's 19 (and python.) Can anyone beat that
Mine is even shorter:
./.py
that's in bash, it runs the python file with
#usr/bin/python3
at the top

Last edited by applejuiceproduc (Feb. 21, 2022 08:27:58)


A signature
mybearworld
Scratcher
1000+ posts

Programming challanges!

That doesn't count

Signatures are the only place where assets links still work.

Powered by DjangoBB