Уведомления

Группа в Telegram: @pythonsu

#1 Дек. 3, 2017 20:00:35

Valera_pyt
Зарегистрирован: 2017-12-03
Сообщения: 2
Репутация: +  0  -
Профиль   Отправить e-mail  

менее повторяемое число в списке

Здравствуйте

Нужна ваша помощь!

Задание: дан список простых чисел. Нужно сделать функцию, которая определит наименее часто встречающееся число. Нельзя использовать продвинутые методы, код должен быть самый базовый как у новичков.
Буду очень признателен любой помощи!!


Given a list of integers, where each integer may appear multiple times, we want to identify the least common integer in the list. If different integers have the same lowest frequency then the result is the one occurring latest in the list. For example, the least common integer in 3, 4, 4, 3 is 3.

Отредактировано Valera_pyt (Дек. 3, 2017 20:14:23)

Офлайн

#2 Дек. 3, 2017 20:22:56

marvellik
Зарегистрирован: 2016-05-15
Сообщения: 639
Репутация: +  73  -
Профиль   Отправить e-mail  

менее повторяемое число в списке

 def min_count(x):
    res = x[0]
    for i in set(x):
        if x.count(res)>x.count(i):
            res = i
    return res

Офлайн

#3 Дек. 4, 2017 03:28:13

py.user.next
От:
Зарегистрирован: 2010-04-29
Сообщения: 10003
Репутация: +  857  -
Профиль   Отправить e-mail  

менее повторяемое число в списке

Valera_pyt
Given a list of integers, where each integer may appear multiple times, we want to identify the least common integer in the list. If different integers have the same lowest frequency then the result is the one occurring latest in the list. For example, the least common integer in 3, 4, 4, 3 is 3.
  
>>> def f(seq):
...     dct = {}
...     for i, e in enumerate(seq):
...         dct[e] = dct.get(e, 0) + 1
...     tmp = None
...     for k, v in dct.items():
...         if tmp is None or v <= tmp[1]:
...             tmp = k, v
...     out = tmp[0]
...     return out
... 
>>> f([1, 1, 2, 2, 3, 3, 4, 4])
4
>>> f([1, 1, 2, 2, 3, 3, 4, 4, 4])
3
>>> f([1, 1, 2, 2, 2, 3, 3, 4, 4, 4])
3
>>>



Отредактировано py.user.next (Дек. 4, 2017 03:34:47)

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version