Decimal to Hex resulting in 0xDecimal number [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to sum a hex number with an int one. Bot numbers are given as strings:
The following code
result = hex(int("12", 16) + int("3", 16))
print(result)
yields:
0x15 # instead of 0xF
Any idea on how to obtain 0xF instead of 0x15?

The answer Python gave you is correct. 0x12 + 3 = 0x15. Your problem is that you read 0x12 as 12 when you calculated it in your head, when it's actually 18.
An additional problem you will have is that you're treating both numbers as hex numbers. You only need int("3") for the second one.

Everything works as expected, you just assumed that 12 in hexadecimal is equal to 12 in decimal which is not.
result = hex(int('C', 16) + int('3', 16))
print(result)
0xC=12
0x12=18

Related

Check if the length of the range divided by range element has no division remainder [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The code below shall show a specific comment if the number of elements in the array isn't integer-type number.
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
How can I change the code to get 4%0.1==0?
import math
limits_real=(-2,2)
dr=0.1
if (limits_real[1]-limits_real[0])%dr!=0:
print ('Inapropriate limits or elements')
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
Because floating point numbers have limitations that every programmer should know, see this:
https://docs.python.org/3/tutorial/floatingpoint.html
As showed at the end of above documentation, you can use decimal module, which works exacly right but is slower than normal floating point arihtmetics:
from decimal import Decimal
limits_real=(-2,2)
dr = Decimal("0.1")
if (limits_real[1] - limits_real[0]) % dr != 0:
print ('Inapropriate limits or elements')
Note that you should use a str while constructing the Decimal instance, do not use a float.
I found out only this solution :) At least it works
import math
limits_real=(-2,2)
dr=0.1
if (((limits_real[1] - limits_real[0]) * 1000) % (dr * 1000)) / 1000 != 0:
print ('Inapropriate limits or elements')

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have to write a function recursively to convert a given decimal string ad return a binary. I can't change the first 3 lines of the function(n = Len(a) ... return str(bin(int(a)))) and I don't understand why my solution doesn't work. Does anyone have any suggestions?
{str} -> {str}
def converttobin(a: str) -> str:
n = len(a)
if n == 1:
return str(bin(int(a)))
else:
return converttobin(str(int(a) % 2)) + str(int(a) % 2)
Instead of sending the remainder, send the quotient str((int)a//2) to the function call.
For decimal to binary conversion, we take remainders as the current output and further apply the conversion on the quotient.

Round is returning (100, 1) instead if 100 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to find the percentage of questions right but my percentage code is returning (100, 1) but I want it to return 100. My code is
accuracy = 100 # Setting a default.
def percentage(part, whole):
return(round((part/whole)*100),1)
and the code to print is
accuracy = percentage(questionsright, questionscompleted)
print("Your accuracy is now " + str(accuracy) + "% .")
Does anybody know why it isn't returning 100? Please Help.
You have parenthesis in the wrong places in this function:
def percentage(part, whole):
return(round((part/whole)*100),1)
Without the change, your function was returning an ordered tuple of two elements where the first element is round((part/whole)*100) and the second is 1.
Here is the corrected function:
def percentage(part, whole):
return round((part/whole)*100,1)

If a = str(integer), do print(len(a)) and return len(a) work the same in a function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am new to programming and have just briefly learned functions in python. I do not understand why print(len(str)) don't work the same as return len(str) in a function.
I have tried both print and return for the last statement of the function and am confused about my understanding of len(). I need some guidance, thank you! Perhaps someone can guide me as to how I can further improve my foundation as I am still pretty new to programming. Thank you!!
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
numDigits(833)
If I change the
print to return len(str_digits) and
numDigits(833) to print(numDigits(833)),
I get my expected answer.
What i expected:
3
Actual result:
3
None
1
2
4
3
In the first case numDigits doesn't return a value from the function, and you only print it inside the function
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
print(numDigits(833))
The output here is
3
None
The 3 comes from print and None comes from the function, and when you print it, it prints None
If you want to return, you need a return statement like return len(str_digits) at the end of the function like so
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
#Return statement
return len(str_digits)
print(numDigits(833))
The output will now be
3
3
Now the first 3 comes from print, and the second 3 comes when you print what numDigits return, which is 3

basic bit flip algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
I am new to programming. I took a basic course in Python, so I know the basic. I am trying to practice a lot more. I am attempting this question and I don't know where to start.
You will be given a list of 32-bits unsigned integers. You are
required to output the list of the unsigned integers you get by
flipping bits in its binary representation (i.e. unset bits must be
set, and set bits must be unset).
Input Format
The first line of the input contains the list size T. T lines follow
each line having an integer from the list.
Constraints
1 ≤ T ≤ 100
Output Format
Output one line per element from the list with the requested result.
Sample Input
3 2147483647 1 0
Sample Output
2147483648 4294967294 4294967295
Explanation
Take 1 for example, as unsigned 32-bits is
00000000000000000000000000000001 and doing the flipping we get
11111111111111111111111111111110 which in turn is 4294967294
Can be done with the bitwise XOR operator, which is ^ in Python.
Example:
a = 0xF0101010
b = 0xFFFFFFFF
print(bin(a))
print(bin(b))
print(bin(a ^ b))
0b11110000000100000001000000010000
0b11111111111111111111111111111111
0b1111111011111110111111101111
foreach x in input:
x_flipped = ~x & 0xffffffff
print "bits flipped in unsigned 32-bit", bin(x_flipped)
Explained:
- (~x) flips all bits
~ (& 0xffffffff) converts 2's complement into unsigned int.

Categories

Resources