나는 이렇게 학습한다/Algorithm & SQL

Coding Meetup #5 - Higher-Order Functions Series - Prepare the count of languages

daco2020 2022. 5. 8. 18:26
반응형

You will be given an array of objects (associative arrays in PHP, table in COBOL) representing data about developers who have signed up to attend the next coding meetup that you are organising.

Your task is to return an object (associative array in PHP, table in COBOL) which includes the count of each coding language represented at the meetup.

For example, given the following input array:

list1 = [
    { 'firstName': 'Noah', 'lastName': 'M.', 'country': 'Switzerland', 'continent': 'Europe', 'age': 19, 'language': 'C' },
    { 'firstName': 'Anna', 'lastName': 'R.', 'country': 'Liechtenstein', 'continent': 'Europe', 'age': 52, 'language': 'JavaScript' },
    { 'firstName': 'Ramon', 'lastName': 'R.', 'country': 'Paraguay', 'continent': 'Americas', 'age': 29, 'language': 'Ruby' },
    { 'firstName': 'George', 'lastName': 'B.', 'country': 'England', 'continent': 'Europe', 'age': 81, 'language': 'C' },
    ]

your function should return the following object (associative array in PHP, table in COBOL):

{ 'C': 2, 'JavaScript': 1, 'Ruby': 1 }

Notes:

  • The order of the languages in the object does not matter.
  • The count value should be a valid number.
  • The input array will always be valid and formatted as in the example above.

 

Solution:

1. Assign language 1 to the dictionary.
2. If language is duplicated, add one by one.
3. Return language and number.

 

def count_languages(lst): 
    dict = {}
    for i in lst:
        language = i.get('language')
        if dict.get(language):
            dict[language] += 1
        else :
            dict[language] = 1
    return dict

 

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Round up to the next multiple of 5  (0) 2022.05.10
Find the capitals  (0) 2022.05.09
Initialize my name  (0) 2022.05.07
Even numbers in an array  (0) 2022.05.06
Number of People in the Bus  (0) 2022.05.05