Website improvements, hackerrank

Website improvements:

Added Catche to my site. It adds instant search. Click on the magnifying glass in the lower right of the screen!

Mapped domain (tomoe.asia) to my current Github Pages site.

Hackerrank

Time conversion

Inelegant solution, but it works. It’s quite late now so I’ll think of a more elegant solution another day:

def timeConversion(s):
    # Write your code
    if s[-2:] == "PM":
        s = s.replace("PM", "")
        digits = int(s[0:2])
        if digits != 12:
            digits += 12
            digits = str(digits)
            s = digits + s[2:]
        else:
            pass

    else:
        s = s.replace("AM", "")
        if s[0:2] == "12":
            s = "00" + s[2:]
    return s

Sparse Arrays

Inelegant attempt that didn’t work (it’s like 2 now so I’m going to just sleep and do this again tomorrow)

def matchingStrings(strings, queries):
    # initialise array to track results
    results = []
    # Make results arr as long as queries
    results = [0 for i in range(len(queries))]
    for query in queries:
        for string in strings:
            if query == string:
                results[queries.index(query)] += 1 
            else: 
                pass
    return results 

Better solutions from syedshahid821721 and VictorSGhosh

{% marginnote “margin-noteid” “I should get a lot more familiar with Python methods. Knowing .count() would’ve saved me so much time.” %}

def matchingStrings(strings, queries):
    # Write your code here
    results=[] 
    count=0
    for i in queries:
        results.append(strings.count(i)) 
    return results
def matchingStrings(strings, queries):
    return [strings.count (q) for q in queries]