Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative solutions for Exercises N36, N81 #214

Open
Artur-Arstamyan opened this issue Sep 25, 2024 · 1 comment
Open

Alternative solutions for Exercises N36, N81 #214

Artur-Arstamyan opened this issue Sep 25, 2024 · 1 comment

Comments

@Artur-Arstamyan
Copy link

Artur-Arstamyan commented Sep 25, 2024

Exercise N36 - Extract the integer part of a random array of positive numbers using 4 different methods
np.modf() returns two arrays: one with the fractional part and one with the integer part.

import numpy as np
Z = np.random.uniform(0,10,10)
print(np.modf(Z)[1])

Exercise N81 - Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]?
Using np.roll function

import numpy as np
Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
print(np.array([np.roll(Z, -i)[:4] for i in range(len(Z)-3)]))
@Artur-Arstamyan Artur-Arstamyan changed the title Alternative solution for Exercise N36 Alternative solutions for Exercises N36, N81 Sep 28, 2024
@rougier
Copy link
Owner

rougier commented Oct 21, 2024

Thanks.

  • For 36, I think the Z%1 provided solution is equivalent
  • For 81, you're creating new arrays, not views.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants