acoustic-carpenter-78188
02/16/2023, 7:47 PMcount_characters
import typing
from time import sleep
from flytekit import dynamic, task, workflow
@task
def return_index(character: str) -> int:
"""
Computes the character index (which needs to fit into the 26 characters list)"""
sleep(1)
if character.islower():
return ord(character) - ord("a")
else:
return ord(character) - ord("A")
@task
def update_list(freq_list: typing.List[int], list_index: int) -> typing.List[int]:
"""
Notes the frequency of characters"""
freq_list[list_index] += 1
return freq_list
@task
def derive_count(freq1: typing.List[int], freq2: typing.List[int]) -> int:
"""
Derives the number of common characters"""
count = 0
for i in range(26):
count += min(freq1[i], freq2[i])
return count
@workflow
def wf1():
return_index(character="h")
@dynamic
def count_characters(s1: str, s2: str) -> int:
freq1 = [0] * 26
freq2 = [0] * 26
# looping through the string s1
for i in range(len(s1)):
# index and freq1 are not accessible as they are promises
index = return_index(character=s1[i])
freq1 = update_list(freq_list=freq1, list_index=index)
# looping through the string s2
for i in range(len(s2)):
# index and freq2 are not accessible as they are promises
index = return_index(character=s2[i])
freq2 = update_list(freq_list=freq2, list_index=index)
wf1()
return derive_count(freq1=freq1, freq2=freq2)
@workflow
def wf(s1: str = "Pear", s2: str = "Earth") -> int:
return count_characters(s1=s1, s2=s2)
if __name__ == "__main__":
print(wf(s1="Pear", s2="Earth"))
Tracking Issue
https://github.com/flyteorg/flyte/issues/
Follow-up issue
NA
flyteorg/flytekit
Codecov: 69.29% (-0.18%) compared to c11362f
✅ 29 other checks have passed
29/30 successful checksacoustic-carpenter-78188
02/16/2023, 7:47 PM