this post was submitted on 01 Sep 2023
2054 points (98.2% liked)

Programmer Humor

32495 readers
849 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 77 points 1 year ago (11 children)

This person misunderstands a beautiful function code can be very sexy or maybe I'm a odd girl.

[–] [email protected] 8 points 1 year ago* (last edited 1 year ago) (9 children)
var LogicGate = map[string]string{
    "OR": "OR",
    "AND":  "AND",
    "NOT": "NOT",
    "NOR": "NOR",
    "NAND": "NOR",
    "XOR": "XOR",
}

func isLogicGate(inString string) (bool) {
    _, ok := LogicGate[strings.ToUpper(inString)]
    if ok {
        return true
    } else {
        return false
    }
}

func stringAsGateLogic(inString string) (bool, error) {
    inSplit := strings.Split(inString, " ")
    var phrase1 strings.Builder
    var phrase2 stringa.Builder
    var gateString string
    for word := range inSplit {
        if isLogicGate(word) {
            if len(gateString) < 1{
                gateString = word
            } else {
                phrase2.WriteString(word)
            }
        } else {
            if len(gateString) < 1{
                phrase1.WriteString(word)
            } else {
                phrase2.WriteString(word)
            }
        }
    }
    boolPhrase1 := bool(phrase1.String())
    boolPhrase2 := bool(phrase2.String())
    switch strings.ToUpper(gateString) {
        case "OR":
            return (boolPhrase1 || boolPhrase2), nil
        case "AND":
            return (boolPhrase1 && boolPhrase2), nil
        case "NOT":
            return (!boolPhrase2), nil
        case "NOR":
            return (!(boolPhrase1 || boolPhrase2)), nil
        case "NAND":
            return (!(boolPhrase1 && boolPhrase2)
        case "XOR":
            orRes := (boolPhrase1 || boolPhrase2)
            nandRes := (!(boolPhrase1 && boolPhrase2))
            return (orRes && nandRes), nil
        default:
            return false, fmt.Errorf("Why you do dis?: %v", inString)
    }
}

func main(){
    answer, err := stringAsGateLogic ("This person misunderstands a beautiful function code can be very sexy or maybe I'm a odd girl.")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(answer)
}
[–] [email protected] 3 points 1 year ago (1 children)

You make want to cry, when are we marrying?

I want a divorce

[–] [email protected] 2 points 1 year ago

Sorry, merge conflict :(

load more comments (7 replies)
load more comments (8 replies)