3. Cleaning Data: Let s be a string that contains a sequence of decimal numbers separated by commas, but somehow the dat
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
3. Cleaning Data: Let s be a string that contains a sequence of decimal numbers separated by commas, but somehow the dat
3. Cleaning Data: Let s be a string that contains a sequence of decimal numbers separated by commas, but somehow the data is corrupted and a random * shows up either right after a comma, before a comma, or before the first term of an integer number. e.g. (that's exempli gratia, latin abbreviation of 'for example’) s = ‘1.23,*2.4*, 3.123,*5.1, 8.7*, *7.23, *3.23*". Write a program that prints the sum of the numbers in the string s. Note, the whitespace is unevenly distributed in the string as well. Do this 2 ways: 1. without using any built in functions or methods associated to the string object but with a for or while loop, and 2. with built in string methods.
=# # # method 1 # # # cleaning data example # # ??? indicates you will need to fill in myStr = '1.23, *2.4*, 3.123, *5.1, 8.7*, *7.23, *3.23*' # subStr = = 0 tally for s in myStr: if s == ',': print(subStr) tally += float(subStr) subStr = elif s == '*'; pass elif ??? ??? else: ??? print(subStr) tally += float(subStr) print(tally) =# = # # # method 2 myStr2 = myStr.replace('*','') print(myStr2) sSplit myStr2.split(',') rint(sSplit) # # you can now use other string methods to finish this up