Home > database >  How to reverse full file with 2 as a limit?
How to reverse full file with 2 as a limit?

Time:12-19

lets take a example file as

11 22 33 44

55 66 77 88

99 00 12 13

and I want to reverse it as

13 12 00 99

88 77 66 55

44 33 22 11

CodePudding user response:

if you have the contents of the file as a string you can use the standard way to reverse a string which is stringVariable[::-1]

CodePudding user response:

# This a a step by step algorithm to get you just that

str_num = "11, 12, 14, 6, 17, 19, 20, 25, 27, 29, 30, 75, 98, 43, 28, 56"
#split by a given delimiter
lst_num = str_num.split(", ")
# reverse the list using the reverse method of the list class
lst_num.reverse()
"join the string back using the delimeter"
reversed_str_num = ", ".join(lst_num)
print(reversed_str_num)


# This is a functional approach just in case the same problem has to 
# be solved severally, remember DRY, Cheers!

def reverse_str(str_to_rev:str="", _delimiter:str=",") -> str:
    str_to_list = str_to_rev.split(_delimiter)
    str_to_list.reverse()
    return ", ".join(str_to_list) or ""

print(reverse_str(str_num))
  • Related