Python code to node.js - node.js

I found this part in a python code:
score = random.randint(0, 100)
filled_progbar = round(score / 100 * 10)
counter_ = '█' * filled_progbar + '‍ ‍' * (10 - filled_progbar)
It looks good in python but I don't know how I can put this in node.js.
Why I need this from python to node.js?
Because I am dumb to python and I am newbie in the node.js and therefore I am asking here. :)
Sorry if I didn't choose the right tags but I am not sure which tag would be good to this.
If my research is correct then the score part would be so:
Math.floor(Math.random() * 100);
But I don't know the other part.

It seems pointless but fun so I gave it a go,
const score = parseInt(Math.random()*100);
const filled_progbar = Math.round(score/10.0);
const counter_ = '█'.repeat(filled_progbar) + '‍ ‍'.repeat(10 - filled_progbar)
console.log(counter_);

Related

Python 3.9.0 - Physics simulation of drag working abnormally

def CalculateAcceleration(self):
currentTheta = math.atan(self.velocity[1]/self.velocity[0])
currentSquaredVelocity = math.pow(self.velocity[0],2)+math.pow(self.velocity[1],2)
accelerationMagnitude = currentSquaredVelocity*self.coeffD
self.acceleration = [math.cos(currentTheta)*accelerationMagnitude, math.sin(currentTheta)*accelerationMagnitude]
self.acceleration = [self.acceleration[0], self.acceleration[1]-self.g]
I believe this is where the problem is and it only happens when the the projectile is going to the right.
not working...
working
I forgot the negative sign in the force calculation

Godot - How can I make my chunks generate around the player in a 3 by 3 grid

How can I generate chunks in a 3 by 3 grid so that there is always 9 grid instances in the scene? Currently I only have 1 chunk generating every time the player moves to a new area/chunk.
To make it easier for you guys I put it into a github repo so you guys can download and try it!
https://github.com/Dragon20C/GODOT---Flat-Terrain-Generation
Relevant code:
var chunk_x = floor(player.translation.x / chunk.size)
var chunk_z = floor(player.translation.z / chunk.size)
var new_chunk_pos = Vector2(chunk_x, chunk_z)
if new_chunk_pos != chunk_pos:
if !new_chunk_pos in previous_chunks:
chunk_pos = new_chunk_pos
var instance = chunk_scene.instance()
add_child(instance)
instance.chunk_position_set(Vector3(chunk_pos.x * chunk.size,0,chunk_pos.y * chunk.size))
previous_chunks.append(chunk_pos)
Extract this code into a new function:
if !new_chunk_pos in previous_chunks:
chunk_pos = new_chunk_pos
var instance = chunk_scene.instance()
add_child(instance)
instance.chunk_position_set(Vector3(chunk_pos.x * chunk.size,0,chunk_pos.y * chunk.size))
previous_chunks.append(chunk_pos)
Then call it passing new_chunk_pos And the positions of the chunks around it. That is the positions with x in the range from new_chunk_pos.x - 1 to new_chunk_pos.x + 1 and with y in the range from new_chunk_pos.y - 1 to new_chunk_pos.y + 1.

Issue producing a valid WIF key with Python 3.6 (Pythonista - iPadOS)

I am having some issues with some code. I have set about a project for creating Bitcoin wallets in an attempt to turn a hobby into a learning experience, whereby I can understand both Python and the Bitcoin protocol in more detail. I have posted here rather than in the Bitcoin site as the question is related to Python programming.
Below I have some code which I have created to turn a private key into a WIF key. I have written this out for clarity rather than the most optimal method of coding, so that I can see all the steps clearly and work on issues. This code was previously a series lines which I have now progressed into a class with functions.
I am following the example from this page: https://en.bitcoin.it/wiki/Wallet_import_format
Here is my current code:
import hashlib
import codecs
class wif():
def private_to_wif(private_key):
extended_key = wif.create_extended(private_key)
address = wif.create_wif_address(extended_key)
return address
def create_extended(private_key):
private_key1 = bytes.fromhex(private_key)
private_key2 = codecs.encode(private_key1, 'hex')
mainnet = b'80'
#testnet = b'ef'
#compressed = b'01'
extended_key = mainnet + private_key2
return extended_key
def create_wif_address(extended_key):
first_hash = hashlib.sha256(extended_key)
first_digest = first_hash.digest()
second_hash = hashlib.sha256(first_digest)
second_digest = second_hash.digest()
second_digest_hex = codecs.encode(second_digest, 'hex')
checksum = second_digest_hex[:8]
extended_key_chksm = (extended_key + checksum).decode('utf-8')
wif_address = base58(extended_key_chksm)
return wif_address
def base58(extended_key_chksm):
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
b58_string = ''
leading_zeros = len(extended_key_chksm) - len(extended_key_chksm.lstrip('0'))
address_int = int(extended_key_chksm, 16)
while address_int > 0:
digit = address_int % 58
digit_char = alphabet[digit]
b58_string = digit_char + b58_string
address_int //= 58
ones = leading_zeros // 2
for one in range(ones):
b58_string = '1' + b58_string
return b58_string
I then use a few lines of code to get this working, using the example private key from the above guide, as follows:
key = ‘0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D‘
address = wif.private_to_wif(key)
Print(address)
I should be getting the output: 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
Instead I’m getting:
5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbWs6eYX
It’s only the last 6 characters that differ!
Any suggestions and help would be greatly appreciated.
Thank you in advance.
Connor
I have managed to find the solution by adding a missing encoding step.
I am posting for all those who run into a similar issue and can see the steps that brought resolution.
def create_wif_address(extended_key):
extended_key_dec = codecs.decode(extended_key, 'hex')
first_hash = hashlib.sha256(extended_key_dec)
first_digest = first_hash.digest()
second_hash = hashlib.sha256(first_digest)
second_digest = second_hash.digest()
second_digest_hex = codecs.encode(second_digest, 'hex')
checksum = second_digest_hex[:8]
extended_key_chksm = (extended_key + checksum).decode('utf-8')
wif_address = base58(extended_key_chksm)
return wif_address
So above I added in a step to the function, at the beginning, to decode the passed in variable from a hexadecimal byte string to raw bytes, which is what the hashing algorithms require it seems, and this produced the result I was hoping to achieve.
Connor

I can't understand this shape from the python code

I'm new to python. I'm searching on google about python code to get street sign detection, I found some code but I can't understand what the code means.
elif dominant_color[0] > 80:
zone_0 = square[square.shape[0]*3//8:square.shape[0]
* 5//8, square.shape[1]*1//8:square.shape[1]*3//8]
cv2.imshow('Zone0', zone_0)
zone_0_color = warnadominan(zone_0, 1)
zone_1 = square[square.shape[0]*1//8:square.shape[0]
* 3//8, square.shape[1]*3//8:square.shape[1]*5//8]
cv2.imshow('Zone1', zone_1)
zone_1_color = warnadominan(zone_1, 1)
zone_2 = square[square.shape[0]*3//8:square.shape[0]
* 5//8, square.shape[1]*5//8:square.shape[1]*7//8]
cv2.imshow('Zone2', zone_2)
zone_2_color = warnadominan(zone_2, 1)
Thanks in advance
Assuming square has shape (8, 8)
I think this diagram might help:
Edit: The code is trying to extract the zones shown in the image

fipy viewer not plotting

from fipy import *
nx = 50
dx = 1.
mesh = Grid1D(nx=nx, dx=dx)
phi = CellVariable(name="solution variable",
mesh=mesh,
value=0.)
D = 1.
valueLeft = 1
valueRight = 0
phi.constrain(valueRight, mesh.facesRight)
phi.constrain(valueLeft, mesh.facesLeft)
eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=D)
timeStepDuration = 0.9 * dx**2 / (2 * D)
steps = 100
phiAnalytical = CellVariable(name="analytical value",
mesh=mesh)
viewer = Viewer(vars=(phi, phiAnalytical),
datamin=0., datamax=1.)
viewer.plot()
x = mesh.cellCenters[0]
t = timeStepDuration * steps
try:
from scipy.special import erf
phiAnalytical.setValue(1 - erf(x / (2 * numerix.sqrt(D * t))))
except ImportError:
print "The SciPy library is not available to test the solution to \
the transient diffusion equation"
for step in range(steps):
eqX.solve(var=phi,
dt=timeStepDuration)
viewer.plot()
I am trying to implement an example from the fipy examples list which is the 1D diffusion problem. but I am not able to view the result as a plot.
I have defined viewer correctly as suggested in the code for the example. Still not helping.
The solution vector runs fine.
But I am not able to plot using the viewer function. can anyone help? thank you!
Your code works fine on my computer. Probably is a problem with a plotting library used by FiPy.
Check if the original example works (just run this from the fipy folder)
python examples/diffusion/mesh1D.py
If not, download FiPy from the github page of the project https://github.com/usnistgov/fipy and try the example again. If not, check if the plotting libraries are correctly installed.
Anyways, you should specify the platform you are using and the errors you are getting. The first time I had some problems with the plotting libraries too.

Resources