E
E
Enot_Star2020-05-08 14:31:27
Python
Enot_Star, 2020-05-08 14:31:27

How to convert a number greater than 6 to septimal number system?

There is the following line in the code: Which gives an error: ValueError: invalid literal for int() with base 7: '82' How to fix this and convert numbers greater than 6 to septal number system?
int(str(82),7)

Answer the question

In order to leave comments, you need to log in

9 answer(s)
E
Enot_Star, 2020-05-08
@Enot_Star

I found the solution myself https://www.cyberforum.ru/python-beginners/thread2...

def dec_to_base(N, base):
    if not hasattr(dec_to_base, 'table'):        
        dec_to_base.table = '0123456789ABCDEF'       
    x, y = divmod(N, base)        
    return dec_to_base(x, base) + dec_to_base.table[y] if x else dec_to_base.table[y]

P
pfg21, 2020-05-08
@pfg21

write a function to convert the number system of a number.
google and sprinkle on you such functions as from a cornucopia. figure it out, think about it, and do it for yourself.
This is what training is based on.

V
Vadim, 2020-05-08
@Viji

Copied code from https://www.quora.com/How-do-I-write-a-program-in-...
def convertToSimeric(number, targetBase=7):
newNum = ''
powers = []
for x in reversed(range(targetBase)):
powers.append(2**x)
for i, x in enumerate(powers):
if number - x >= 0:
newNum += '1'
number = number - x
else:
newNum += '0'
print(''.join(newNum))

F
FanatPHP, 2013-11-18
@codercat

Looked at the request more closely.
It seems to me that the main problem here is sorting the main table by a non-indexed field.
I suggest to start with making an index on the price field.
ALTER TABLE tasks ADD INDEX (status,price);
moreover, since reverse sorting is needed, and mysql does not support it in indexes, then store the price in negative values, and take it modulo when extracting. For now, just remove DESC from the query for tests.

V
Vladimir Boliev, 2013-11-18
@voff

Add another status to the tasks table (you have one, but it's not clear why you need it). Let it be the status1 field, which has the value 0 - if the task is not hidden and not completed, 1 - if it is hidden, 2 - if it is completed. Now you can take not hidden and not completed tasks simply by status, discarding two subrequests at once.

S
Stepan, 2013-11-18
@L3n1n

Did you make an explain request?
Using just IN is not very recommended, but you generally have IN (SELECT ...)
Explain your query and see how many rows are affected. In your case, it will be several million sorts.
Do as you are advised above. Add a field to tasks and make it an index. Make the field tinyint or set('done','hide') so that it doesn't take up much space.

D
Dmitry Goryachev, 2013-11-18
@Gordim

Can you show what explain shows?
What version of mysql?
show my.cnf

K
Kirill Firsov, 2013-11-18
@Isis

Try adding an index

ALTER table tasks ADD index test_index (`id`, `status`, `price`, `name`);

and show EXPLAIN again.

P
Petrusha Ukropov, 2013-11-18
@artishok

Merge the done and hide tables into one, such as tasks_status

CREATE TABLE IF NOT EXISTS `tasks_status` (
  `task_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
`status` int(1) NOT NULL,
  UNIQUE KEY `task_id_user_id` (`task_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Where the task status will be stored, as voff wrote, statuses - 0,1,2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question