print("#1 as given in solution")
print("-----------------","\n")
list_of_test_apps = ["Instagram", "爱奇艺PPS -《欢乐颂2》电视剧热播", "Docs To Go™ Free Office Suite",
"Inst😜achat 😜_😜艺😜", "'奇艺PPS_testing", "《欢奇艺PPS_testing more"]
def ASCII_Check(string_param):
non_ascii = 0
for element in string_param:
if ord(element) > 127:
print("element:",element, "|non ascii count:", non_ascii)
non_ascii += 1
if non_ascii > 3:
return (False, "total non ascii count:", non_ascii, "| last element:", element)
else:
return (True, "total non ascii count:", non_ascii, "| last element:", element)
for each in list_of_test_apps:
print("app name:",each)
print("function result:",ASCII_Check(each), "\n")
#1 as given in solution ----------------- app name: Instagram function result: (True, 'total non ascii count:', 0, '| last element:', 'm') app name: 爱奇艺PPS -《欢乐颂2》电视剧热播 element: 爱 |non ascii count: 0 element: 奇 |non ascii count: 1 element: 艺 |non ascii count: 2 element: 《 |non ascii count: 3 element: 欢 |non ascii count: 4 element: 乐 |non ascii count: 5 element: 颂 |non ascii count: 6 element: 》 |non ascii count: 7 element: 电 |non ascii count: 8 element: 视 |non ascii count: 9 element: 剧 |non ascii count: 10 element: 热 |non ascii count: 11 element: 播 |non ascii count: 12 function result: (False, 'total non ascii count:', 13, '| last element:', '播') app name: Docs To Go™ Free Office Suite element: ™ |non ascii count: 0 function result: (True, 'total non ascii count:', 1, '| last element:', 'e') app name: Inst😜achat 😜_😜艺😜 element: 😜 |non ascii count: 0 element: 😜 |non ascii count: 1 element: 😜 |non ascii count: 2 element: 艺 |non ascii count: 3 element: 😜 |non ascii count: 4 function result: (False, 'total non ascii count:', 5, '| last element:', '😜') app name: '奇艺PPS_testing element: 奇 |non ascii count: 0 element: 艺 |non ascii count: 1 function result: (True, 'total non ascii count:', 2, '| last element:', 'g') app name: 《欢奇艺PPS_testing more element: 《 |non ascii count: 0 element: 欢 |non ascii count: 1 element: 奇 |non ascii count: 2 element: 艺 |non ascii count: 3 function result: (False, 'total non ascii count:', 4, '| last element:', 'e')
print("#2 inside for loop outside first if block - Doesn't Work")
print("-----------------","\n")
list_of_test_apps = ["Instagram", "爱奇艺PPS -《欢乐颂2》电视剧热播", "Docs To Go™ Free Office Suite",
"Inst😜achat 😜_😜艺😜", "'奇艺PPS_testing", "《欢奇艺PPS_testing more"]
def ASCII_Check(string_param):
non_ascii = 0
for element in string_param:
if ord(element) > 127:
print("element:",element, "|non ascii count:", non_ascii)
non_ascii += 1
if non_ascii > 3:
return (False, non_ascii, element)
else:
return (True, non_ascii, element)
for each in list_of_test_apps:
print("app name:",each)
print("function result:",ASCII_Check(each), "\n")
#2 inside for loop outside first if block - Doesn't Work ----------------- app name: Instagram function result: (True, 0, 'I') app name: 爱奇艺PPS -《欢乐颂2》电视剧热播 element: 爱 |non ascii count: 0 function result: (True, 1, '爱') app name: Docs To Go™ Free Office Suite function result: (True, 0, 'D') app name: Inst😜achat 😜_😜艺😜 function result: (True, 0, 'I') app name: '奇艺PPS_testing function result: (True, 0, "'") app name: 《欢奇艺PPS_testing more element: 《 |non ascii count: 0 function result: (True, 1, '《')
print("""#3 nested inside first if block - Doesn't work!""")
print("-----------------","\n")
list_of_test_apps = ["Instagram", "爱奇艺PPS -《欢乐颂2》电视剧热播", "Docs To Go™ Free Office Suite",
"Inst😜achat 😜_😜艺😜", "'奇艺PPS_testing", "《欢奇艺PPS_testing more"]
def ASCII_Check(string_param):
non_ascii = 0
for element in string_param:
if ord(element) > 127:
print("element:",element, "|non ascii count:", non_ascii)
non_ascii += 1
if non_ascii > 3:
return (False, non_ascii, element)
else:
return (True, non_ascii, element)
for each in list_of_test_apps:
print("app name:",each)
print("function result:",ASCII_Check(each), "\n")
#3 nested inside first if block - Doesn't work! ----------------- app name: Instagram function result: None app name: 爱奇艺PPS -《欢乐颂2》电视剧热播 element: 爱 |non ascii count: 0 function result: (True, 1, '爱') app name: Docs To Go™ Free Office Suite element: ™ |non ascii count: 0 function result: (True, 1, '™') app name: Inst😜achat 😜_😜艺😜 element: 😜 |non ascii count: 0 function result: (True, 1, '😜') app name: '奇艺PPS_testing element: 奇 |non ascii count: 0 function result: (True, 1, '奇') app name: 《欢奇艺PPS_testing more element: 《 |non ascii count: 0 function result: (True, 1, '《')
print("""#4 as suggested by @biadboze - The final result matches that of solution,
although the Total count of non ascii characters is not equal but till the times its less than or equal to 3 it should work
and give correct number of apps when applied to the dataset""")
print("-----------------","\n")
list_of_test_apps = ["Instagram", "爱奇艺PPS -《欢乐颂2》电视剧热播", "Docs To Go™ Free Office Suite",
"Inst😜achat 😜😜艺😜", "'奇艺PPS_testing", "《欢奇艺PPS_testing more"]
def ASCII_Check(string_param):
non_ascii = 0
for element in string_param:
if ord(element) > 127:
print("element:",element, "|non ascii count:", non_ascii)
non_ascii += 1
if non_ascii > 3:
return (False, non_ascii, element)
return (True, non_ascii, element)
for each in list_of_test_apps:
print("app name:",each)
print("function result:",ASCII_Check(each), "\n")
#4 as suggested by @biadboze - The final result matches that of solution, although the Total count of non ascii characters is not equal bit till its less than or equal to 3 it should work and give correct number of apps when applied to the dataset ----------------- app name: Instagram function result: (True, 0, 'm') app name: 爱奇艺PPS -《欢乐颂2》电视剧热播 element: 爱 |non ascii count: 0 element: 奇 |non ascii count: 1 element: 艺 |non ascii count: 2 element: 《 |non ascii count: 3 function result: (False, 4, '《') app name: Docs To Go™ Free Office Suite element: ™ |non ascii count: 0 function result: (True, 1, 'e') app name: Inst😜achat 😜😜艺😜 element: 😜 |non ascii count: 0 element: 😜 |non ascii count: 1 element: 😜 |non ascii count: 2 element: 艺 |non ascii count: 3 function result: (False, 4, '艺') app name: '奇艺PPS_testing element: 奇 |non ascii count: 0 element: 艺 |non ascii count: 1 function result: (True, 2, 'g') app name: 《欢奇艺PPS_testing more element: 《 |non ascii count: 0 element: 欢 |non ascii count: 1 element: 奇 |non ascii count: 2 element: 艺 |non ascii count: 3 function result: (False, 4, '艺')