Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added another array problem named reverse_array_in_groups #32

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

Open
krishnavamshidevandla wants to merge 2 commits into MTrajK:master
base: master
Choose a base branch
Loading
from krishnavamshidevandla:master

Conversation

Copy link

@krishnavamshidevandla krishnavamshidevandla commented Aug 4, 2023

Added another problem of DSA of array where you reverse sub-arrays in the main array according to given sub-array length

@krishnavamshidevandla krishnavamshidevandla changed the title (削除) Added another array problem names reverse_array_in_groups (削除ここまで) (追記) Added another array problem named reverse_array_in_groups (追記ここまで) Aug 4, 2023
Copy link
Owner

MTrajK commented Aug 5, 2023

Okay, I have tried it and it looks like this:

def reverse_array_in_groups(arr, k):
 i = 0
 n = len(arr)
 group = 0
 group_size = k
 
 while i<n:
 if (i - group*k == k):
 group += 1
 if (n - i < k):
 group_size = n - i
 
 if (i - group*k < group_size//2):
 right = group*k + (group_size - 1 - (i - group*k))
 left = i
 
 print(i, arr[i], right)
 arr[left], arr[right] = arr[right], arr[left]
 
 i += 1
 
 return arr

But it can be simplified and refactored (some of the formulas can be simplified and some of the checks).

@krishnavamshidevandla could you please try it? thanks.

krishnavamshidevandla reacted with thumbs up emoji

Copy link
Owner

MTrajK commented Aug 5, 2023

You could also add both of the solutions in that file.

Copy link
Author

@MTrajK I have tried your solution, it's working fine. But I can't get my mind around it. Can you edit the solution with a detailed explanation using comments between the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /