Всем привет.
Простая задачка - подсчет номера ячейки в документах LibreOffice Calc, т.е. перевод из буквенного представления в число.
Задачка для python. Несложная, но было бы любопытно взглянуть на красивое решение.
Как вы помните, после Z(26) идет AA(27), потом AB(28) и т.д. Код должен работать и для любого столбца.
номера LibreOffice Calc столбцов (задачка для разминки в python)
Модератор: Модераторы разделов
- Stauffenberg
- Сообщения: 2042
- Статус: ☮ PEACE ☮
- ОС: открытая и свободная
номера LibreOffice Calc столбцов
Labor omnia vincit
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
Re: номера LibreOffice Calc столбцов
А почему не воспользоваться стандартной функцией LOCalc:
=COLUMN(AF4)
32
?
=COLUMN(AF4)
32
?
Re: номера LibreOffice Calc столбцов
Python, чтобы было непонятно:
Код: Выделить всё
sum(map(lambda x: 26**x[0] * (ord(x[1]) - 64), enumerate(reversed("AFZ"))))
Спасибо сказали:
- Stauffenberg
- Сообщения: 2042
- Статус: ☮ PEACE ☮
- ОС: открытая и свободная
Re: номера LibreOffice Calc столбцов
По идее мы обрабатываем ячейки из python-скрипта, не внося никаких изменений в сам документ.
Labor omnia vincit
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
- Stauffenberg
- Сообщения: 2042
- Статус: ☮ PEACE ☮
- ОС: открытая и свободная
Re: номера LibreOffice Calc столбцов
NickLion писал(а): ↑28.05.2015 18:22Python, чтобы было непонятно:
Код: Выделить всё
sum(map(lambda x: 26**x[0] * (ord(x[1]) - 64), enumerate(reversed("AFZ"))))
Немного изменил, чтобы легче было тестировать:
Код: Выделить всё
#!/usr/bin/env python
import sys
def let_num(string):
return sum(map(lambda x: 26**x[0] * (ord(x[1]) - 64), enumerate(reversed(str(string)))))
print "a = ", let_num(sys.argv[1])
Какое значение возвращает, к примеру, BB? 54? А какое должно? (:
Labor omnia vincit
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
- Bizdelnick
- Модератор
- Сообщения: 20793
- Статус: nulla salus bello
- ОС: Debian GNU/Linux
Re: номера LibreOffice Calc столбцов
Stauffenberg писал(а): ↑29.05.2015 11:20Какое значение возвращает, к примеру, BB? 54? А какое должно?
Да вроде 54 и должно, не? Проверил в уме, проверил в LO — всё сходится.
Пишите правильно:
в консоли вку́пе (с чем-либо) в общем вообще | в течение (часа) новичок нюанс по умолчанию | приемлемо проблема пробовать трафик |
Спасибо сказали:
- Stauffenberg
- Сообщения: 2042
- Статус: ☮ PEACE ☮
- ОС: открытая и свободная
Re: номера LibreOffice Calc столбцов
Bizdelnick писал(а): ↑29.05.2015 11:27Stauffenberg писал(а): ↑29.05.2015 11:20Какое значение возвращает, к примеру, BB? 54? А какое должно?
Да вроде 54 и должно, не? Проверил в уме, проверил в LO — всё сходится.
Сорри, да, все верно...
Labor omnia vincit
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” (Brian Kernighan)