Home > OS >  How can I convert the contents of a list to upper without using .upper()?
How can I convert the contents of a list to upper without using .upper()?

Time:06-29

I'm not allowed to use .upper in my code but i'm lost as to why this doesnt work. some of my inputs are "AgtGtGcC" "AAtcccgG"

import sys
seqs = []
for i in range(len(sys.argv)):
    seq = sys.argv
    seqs.append(seq)
def uppercase(seq):
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = ''
    for x in seq:
        for pos in range(52):
            if alphabet[pos] == x:
                i = pos
        if x not in alphabet or i>=26:
            result  = x
        else:
            result  = alphabet[i 26]
    return result
uppercase(seq)
    
print(seqs)

CodePudding user response:

I think your function does work (unless you will encounter non alphabetic char).

However your script dosen't do what you think. look at this lines in the end:

uppercase(seq)
    
print(seqs)

You are only running your function on one example, and you print the test strings and not the results. Maybe you should change it to something like this:

for seq in seqs:
    print(uppercase(seq))

CodePudding user response:

Here's a lower-level (C-esque) approach using the ASCII table and the map and ord functions.

The function operates by testing the passed character is in fact, lower case (so you don't end up with symbols in the converted value). If the character is lower case, it's converted to upper case; otherwise the original character is returned.

Finally, the string is mapped (via the map function) against the upper function, where the string is joined together for the final result.

Example code:

def upper(c):
    """Convert lower case to upper case."""
    if 97 <= ord(c) <= 122:
        return chr(ord(c)-32)
    else:
        return c

string = 'AgtGtGcC'
result = ''.join(map(upper, string))

Output:

'AGTGTGCC'

Documentation references:

CodePudding user response:

The logic of this is simple: if the character is lowercase, convert to the corresponding uppercase, otherwise, include it as is.

def uppercase(seq):
    lowers = 'abcdefghijklmnopqrstuvwxyz'
    uppers = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = ''
    for x in seq:
        if x in lowers:
            result  = uppers[lowers.index(x)]
        else:
            result  = x
    return result

seqs = 'Hello, How Are You?'
uppercase(seqs)
 # HELLO, HOW ARE YOU?

CodePudding user response:

You can work with ASCII characters to convert lower case character to upper case character. Only point to note over here is you should know how ASCII characters work.

CodePudding user response:

The function works good but you mess up with command line interface, here the doc for sys. Here how to fix it:

File: upper.py

def uppercase(seq):
    # original implementation of the OP
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = ''
    for x in seq:
        for pos in range(52):
            if alphabet[pos] == x:
                i = pos
        if x not in alphabet or i>=26:
            result  = x
        else:
            result  = alphabet[i 26]
    return result


if __name__ == "__main__":
    import sys

    for seq in sys.argv[1:]: # the 0 entry is always the file name, so skip it!
        print(uppercase(seq))

From the command line:

$ python3 upper.py 1212 check check: hallo tHere!

Output

1212
CHECK
CHECK:
HALLO
THERE!
  • Related