Questions tagged [divmod]

Returns an array containing the quotient and modulus obtained by dividing num by numeric.

Returns an array containing the quotient and modulus obtained by dividing num by numeric.

If q, r = x.divmod(y), then

q = floor(x/y)
x = q*y+r

The table below displays the difference between div, divmod, module and remainder methods is displayed below:

enter image description here

Examples:

11.divmod(3)         #=> [3, 2]
11.divmod(-3)        #=> [-4, -1]
11.divmod(3.5)       #=> [3, 0.5]
(-11).divmod(3.5)    #=> [-4, 3.0]
(11.5).divmod(3.5)   #=> [3, 1.0]

Official documentation

20 questions
35
votes
3 answers

Is divmod() faster than using the % and // operators?

I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the…
smichak
  • 4,716
  • 3
  • 35
  • 47
9
votes
2 answers

Does java have a divmod instruction?

Besides divmod being a native instruction on many chipsets, it also is a lot easier on the eyes when subdividing a number in multiple different denominations (which happens with e.g. milliseconds -> datetime conversion, or cents -> coin denomination…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
7
votes
1 answer

Why are negative numbers rounded down after division in Ruby?

I am looking through a documentation on divmod. Part of a table showing the difference between methods div, divmod, modulo, and remainder is displayed below: Why is 13.div(-4) rounded to -4 and not to -3? Is there any rule or convention in Ruby to…
gotqn
  • 42,737
  • 46
  • 157
  • 243
4
votes
1 answer

Divmod algorithm in brainfuck

Can somebody please explain this code to me? I understand what it does but I don't understand how it works. # >n 0 d [->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<] # >0 n d-n%d n%d n/d
Dr. John A Zoidberg
  • 1,168
  • 2
  • 14
  • 25
3
votes
1 answer

Convert numpy array of seconds to minutes and seconds

Here is a good stack overflow question on how to go from seconds to hours, minutes and seconds: How do I convert seconds to hours, minutes and seconds? However, I couldn't find how to convert a numpy array of seconds to minutes:seconds. I have a…
NumesSanguis
  • 5,832
  • 6
  • 41
  • 76
2
votes
1 answer

What is it that this #divmod method is doing to output this result?

I have two arrays: a = [7, 1, 65, 4, 13, 97] b = [] and I'm trying to append the #divmod return values of each of a's elements into b, via the following code: b << a.map(&4.method(:divmod)) I was expecting the results to be: b = [[1, 3], [0, 1],…
jbk
  • 1,911
  • 19
  • 36
2
votes
1 answer

Python divmod for every value in pandas Dataframe

I've got a pandas dataframe that looks like this: P-101 P-103 P-104 P-107 P-114 P-120 P 2415 2535 3345 5650 2805 6210 S 0 45 3105 1165 0 0 D 0 690 690 570 255 830 I want to apply a…
Charon
  • 2,344
  • 6
  • 25
  • 44
1
vote
1 answer

Python numpy.divmod and integer representation

I was trying to use numpy.divmod with very large integers and I noticed a strange behaviour. At around 2**63 ~ 1e19 (which should be the limit for the usual memory representation of int in python 3.5+), this happens: from numpy import divmod test =…
pazqo
  • 477
  • 3
  • 9
0
votes
0 answers

Performance issue with Python code using bit shifting

I am developing a Python application to determine divisibility by repeatedly truncating the final digits of dividend and transforming it to a smaller number. I am running Python 3.7.10 under Windows 10 using a P5 processor. The code was developed…
0
votes
1 answer

Break number into units with custom base

It's a really simple question but I don't understand where I'm wrong. Let's say there's an array of numbers A = [a, b, c, d, ...], and each element is in range [0, N). I want to convert this array to a single number (and back), almost like if these…
user37741
  • 370
  • 1
  • 10
0
votes
3 answers

Replace divmod() by for loop to get quotient and remainder

I am a newbie in python, idk how to do the following question, please help..... Many programming languages implement a function called divmod() (short for division and modulo), which returns both the quotient and the remainder when dividing two…
0
votes
2 answers

Python Divmod help for a mathematical exercise for beginner

So basically all I am trying to do is take the third and forth digits from a set of numbers: # Number Set num_set = (28,54,79,37,2,9,0) and divide them both(79 and 37), This is the code I have written: # Division of third and fourth digits #…
MKN2026
  • 3
  • 1
0
votes
1 answer

Format duration in minutes as weeks, days, hours, minutes

I've got a list of elapsed times in minutes and I'm trying to reformat them as strings of weeks, days, hours and minutes. So, for example, 7,480 minutes = 3 weeks, 4 hours and 40 minutes. I'd like the format to be like so: '3w4h40m' Using divmod…
Charon
  • 2,344
  • 6
  • 25
  • 44
0
votes
1 answer

list indices must be integers or slices, not float

I have a list, sortedInfected, which is made up of integers and with an unknown length. When I run this script i get the error: "list indices must be integers or slices, not float". How can i fix this? medianList =[] b = (len(sortedInfected) /…
Peter
  • 13
  • 1
  • 2
0
votes
4 answers

Taking an input of any length of number and splitting them one character each

I'm a few weeks into learning python and I am trying to write a script that takes an input of any length of numbers and splits them in one-character lengths. like this: input: 123456 output: 1 2 3 4 5 …
1
2