top of page

CCC 2015 J2: Happy or Sad


Comparing elements in a string and using a counter. Using if and else-if to determine the output


def mood(a):
    index = 0
    happy = 0
    sad = 0
    while index < len(a):
        if index + 2 < len(a) and a[index] == ':'/ 
        and a[index+1] == '-' and a[index+2] == ')':
            happy +=1
        elif index + 2 < len(a) and a[index] == ':'/
        and a[index+1] ==  '-' and a[index+2] == '(':
            sad +=1
        index += 1
    if happy > sad:
        print("happy")
    elif happy < sad:
        print("sad")
    elif happy == sad and happy != 0 or sad !=0:
        print("unsure")
    else:
        print("none")

A = input()
mood(A)

Recent Posts

See All

CCC '24 J5 - Harvest Waterloo

#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c, sr, sc; cin >> r; cin >> c; int p[r][c]; bool v

CCC '24 J4 - Troublesome Keys

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { string ps; string ds; cin >> ps; cin >> ds;

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int, int> b){ return a.first < b.first; } bool colcom(pair<int,

Komentarze


bottom of page