I have an assignment that's asking me to get count without a for loop and the test environment keeps returning: Expected count to be 0, but got Params(tuple=(1, 'a', True), expected=0).
Assume tpl to be a tuple containing elements of different types (int, string, float and bool). Write some code that counts how many elements have the same type as the first element. Store the result in a variable called count. Do not use a for loop.
# Get the type of the first element
first_type = type(tpl[0])
# Use list comprehension and the sum function to count matching types
count = sum(1 for elem in tpl if type(elem) == first_type)
# Output the count
print(count)
I have an assignment that's asking me to get count without a for loop and the test environment keeps returning: Expected count to be 0, but got Params(tuple=(1, 'a', True), expected=0).
Assume tpl to be a tuple containing elements of different types (int, string, float and bool). Write some code that counts how many elements have the same type as the first element. Store the result in a variable called count. Do not use a for loop.
# Get the type of the first element
first_type = type(tpl[0])
# Use list comprehension and the sum function to count matching types
count = sum(1 for elem in tpl if type(elem) == first_type)
# Output the count
print(count)
Share
Improve this question
edited Nov 20, 2024 at 1:49
InSync
10.9k4 gold badges17 silver badges56 bronze badges
asked Nov 19, 2024 at 20:45
BoiledbiscuitBoiledbiscuit
11
8
|
Show 3 more comments
3 Answers
Reset to default 1Two more ways:
tpl = 3, 'one', 4, True, 5, 9.
a, *b = map(type, tpl)
count = b.count(a)
print(count)
count = 0
ts = map(type, tpl)
t = next(ts)
while t in ts:
count += 1
print(count)
Attempt This Online!
Assuming tpl
a tuple of mixed types, you could write a recursive method as shown below.
def loop(tpl, target=None):
if not tpl:
return 0
if target is None:
target=type(tpl[0])
return int(type(tpl[0]) == target) + loop(tpl[1:], target)
Subtract 1
from the answer if you want to exclude the first element.
Another way to do it without slicing is to pass an index i
indicating the element to test against the target
. I’ll leave that as an exercise for you.
Well, I finally figured out the answer to this. I had to use a while loop and not count the first iteration of the first type. I did that by setting the index to 1.
typeFirst = type(tpl[0])
index = 1
count = 0
while index < len(tpl):
if type(tpl[index]) == typeFirst:
count += 1
index += 1
print(count)
tpl[1:]
, instead of the whole thing. – BTables Commented Nov 19, 2024 at 20:53for
) counts as afor
loop. It is the same keyword, but not at all the same syntax. A bit like1 if cond else 2
is a ternary operator, not aif
statement. But sure, we lack some context about what that "don't use for loop" means. It usually means "you can't try to outsmart the teacher: use whatever thing we have just learned". For anybody following the lesson it is obvious. For us... – chrslg Commented Nov 19, 2024 at 23:02while
. Maybe if follows a lesson on lambdas, in which cas something likesum(map(lambda x: type(x)==type(tpl[0]), tpl))
is maybe expected (even tho, imho, the list comprehensin is the pythonic way to do that) – chrslg Commented Nov 19, 2024 at 23:05