0

I have a task to write a function that adds "Hello, " and "Привет" if a name in the list is English or Russian and then returns a tuple. For example, we have ["Mary", "Kate", "Маша", "Alex"]. Our function should return a tuple like this: ('Hello, Mary', 'Hello, Kate', 'Привет, Маша', 'Hello, Alex). I have no idea how to achieve this. I can add Hello to all elements, but what to do with this Привет I don't know.

What I came up with so far...

Please help!

def name(my_list):
    for x in my_list:
        new_lis = ["Hello, " + x for x in my_list]
    new_lis1 = tuple(new_lis)
    print(new_lis1)

name(my_list)
  • 1
    How do you know if a name is Russian? – user1558604 Dec 06 '19 at 18:28
  • If it is written with letters from different alphabet, not "abcdefg.." but "абвгдеёжзийклмнопрстуфхцчшщэюя". – Angelina Parfenova Dec 06 '19 at 18:33
  • You can use `x.isalpha()` to test if the name is plain ASCII, but depending on your actual data, this may not be enough. – John Gordon Dec 06 '19 at 18:35
  • 3
    I think @user1558604 means "how would YOU determine, in the code, whether a string is in Russian or English?" Your answer to yourself (in regular English or Russian) will help you write the code itself (i.e. in Python). – Lightning Dec 06 '19 at 18:36
  • 1
    Are you unsure how to *use* a condition to select ``"Hello"`` versus ``"Привет"``, or are you unsure how *define* a condition to check for English versus Russian names? – MisterMiyagi Dec 06 '19 at 19:15

1 Answers1

1

this isn't really the type of website, were you post your question and someone else finds the answer for you - but you are in luck, and someone had the exact same question before.

Following the answers in the linked thread, you could do something like:

def salutation_name(name):
    if all([c in '[а-яА-Я]' for c in name]):
        return f'Привет {name}'
    else:
        return f'Hello {name}'

names = ["Mary", "Kate", "Маша", "Alex"]

print([salutation_name(name) for name in names])
PD7
  • 96
  • 5
  • 1
    Hi PD7. You mean the question is a duplicate. If you just leave a comment to that extent, citing the dupe target, other [users will vote to close as duplicate](https://stackoverflow.com/help/privileges/close-questions) – smci Dec 06 '19 at 18:56
  • Well it wasn't a duplicate, it was just the same problem - thanks for reminding me however. – PD7 Dec 06 '19 at 19:01
  • PD7: essentially it is a dupe, the rest is trivial (anyway it can be done with a list-comprehension). Hey by the way you could change your function to `salutation(text)` and have it directly return `"Привет"/"Hello"` according as `text` has any Cyrillic characters or not. – smci Dec 06 '19 at 19:19
  • Yeah - I was so focused on using part of the other thread as answer, that I completely overlooked that ... – PD7 Dec 06 '19 at 19:30
  • Here's a nicer cleaner decomposition, also doesn't need regex: `def salutation_name(name): if [all(c in '[а-яА-Я]') for c in name]: return f'Привет {name}' else: return f'Hello {name}' for name in names: print salutation_name(name` – smci Dec 06 '19 at 19:36
  • No problem, just wanted to figure out a nice clean decomposition. Also I wonder whether the regex version is faster than non-regex (EDIT: neglecting the once-off overhead to import re and compile the regex) EDIT: on a list length O(N) in general, not just a short list of four names – smci Dec 06 '19 at 19:38