Syntax Coloring

I'm Max. My interests include programming, aeronautics, astronautics and linguistics.

chelonaut:

rektum:

chelonaut:


Hey again! So this is a picture of the code that I did for the intersecting sets. I’m not allowed to use any built in functions, like append, and also not allowed to use the in operator as well, but obviously did in my solution.
It needs to work with letters or numbers or anything with a set and the sets have to be of any length, etc.
Would there be a way to solve this with slicing or recursion? 

You’re seriously not allowed to use append? That’s so strange. I’m stumped, to be honest. Sorry.

it’s cool! thanks for taking the time to look! do you think you could show me how to do it without using the in operator, but using append and whatever else?


Not very elegant, but it works.

Isn’t len() a built-in function?

You were on the right track with slicing and recursion, I think.  Here’s how I’d do it:

# Returns True if haystack contains needle, or False otherwise.
def contains(haystack, needle):
    return (haystack and (haystack[0] == needle or contains(haystack[1:], needle)))

# Prints all the elements that s1 and s2 share.
def printIntersections(s1, s2):
    if (s1):
        if contains(s2, s1[0]): print "Element is in both sets:", s1[0]
        printIntersections(s1[1:], s2)

# Same as above, but also warns of empty sets.
def intersections(s1, s2):
    if not s1: print "Set 1 has no values."
    if not s2: print "Set 2 has no values."
    printIntersections(s1, s2)


(Runnable, syntax-highlighted code here.)

This doesn’t use any built-in functions and it doesn’t use the in operator.

chelonaut:

rektum:

chelonaut:

Hey again! So this is a picture of the code that I did for the intersecting sets. I’m not allowed to use any built in functions, like append, and also not allowed to use the in operator as well, but obviously did in my solution.

It needs to work with letters or numbers or anything with a set and the sets have to be of any length, etc.

Would there be a way to solve this with slicing or recursion? 

You’re seriously not allowed to use append? That’s so strange. I’m stumped, to be honest. Sorry.

it’s cool! thanks for taking the time to look! do you think you could show me how to do it without using the in operator, but using append and whatever else?

Not very elegant, but it works.

Isn’t len() a built-in function?

You were on the right track with slicing and recursion, I think. Here’s how I’d do it:

# Returns True if haystack contains needle, or False otherwise.
def contains(haystack, needle):
    return (haystack and (haystack[0] == needle or contains(haystack[1:], needle)))

# Prints all the elements that s1 and s2 share.
def printIntersections(s1, s2):
    if (s1):
        if contains(s2, s1[0]): print "Element is in both sets:", s1[0]
        printIntersections(s1[1:], s2)

# Same as above, but also warns of empty sets.
def intersections(s1, s2):
    if not s1: print "Set 1 has no values."
    if not s2: print "Set 2 has no values."
    printIntersections(s1, s2)

(Runnable, syntax-highlighted code here.)

This doesn’t use any built-in functions and it doesn’t use the in operator.

The Measure »

roomthily:

it’s a interior book design/typesetting contest. not words i’d ever think to link together.

will this fail spectacularly? probably not. it’ll probably fail silently!

Overcaffeinated people tend to think they’re more skilled than they actually are. I call it the Dunning-Keurig effect.