Playing Around on LeetCode

Enjoy the madness
class Solution(object):
def countMatches(self, items, ruleKey, ruleValue):
"""
:type items: List[List[str]]
:type ruleKey: str
:type ruleValue: str
:rtype: int
"""
match_rule_count = 0
rule_indexes = {
'type': 0,
'color': 1,
'name': 2
}
rule_index = rule_indexes[ruleKey]
for item in items:
if item[rule_index] == ruleValue:
match_rule_count += 1
return match_rule_count
class Solution(object):
def countMatches(self, i, k, v):
c, s = 0, {'type': 0,'color': 1,'name': 2}
a = s[k]
for j in i:
if j[a] == v:
c += 1
return c
Slower runtime, but better memory usage by .1
class Solution(object):
def countMatches(self, i, k, v):
c, s = 0, {'t': 0,'c': 1,'n': 2}
a = s[k[0]]
for j in i:
if j[a] == v:
c += 1
return c
class Solution(object):
def countMatches(self, i, k, v):
c = 0
for j in i:
if (k[0] == 't' and j[0] == v) or (k[0] == 'c' and j[1] == v) or (k[0] == 'n' and j[2] == v):
c += 1
return c
Maybe I got lucky with this run

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store