Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to split an array and add the first part to end in Ruby

1 Answer

0 votes
class CArray 
    def dispay(arr) 
        size = arr.length
        i = 0
        while (i < size) 
            print(arr[i], " ")
            i += 1
        end
    end
    def reverse(arr, start, last) 
        temp = 0
        i = start
        j = last
        while (i <= last && j > i) 
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp
            i += 1
            j -= 1
        end
    end
    def split(arr, split_point) 
        size = arr.length
        if (size <= 1 && split_point < 1 && split_point >= size) 
            return
        end
        # reverse first part
        self.reverse(arr, 0, split_point - 1)
         
        # reverse second part
        self.reverse(arr, split_point, size - 1)
         
        # reverse all array 
        self.reverse(arr, 0, size - 1)
    end
end
 
 
obj = CArray.new()
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
split_point  = 3
 
obj.split(arr, split_point)

obj.dispay(arr)
 
 
 
 
 
# run:
#
# 4 5 6 7 8 9 0 1 2 3 
#

 



answered Nov 30, 2021 by avibootz
edited Nov 30, 2021 by avibootz

Related questions

1 answer 172 views
2 answers 246 views
1 answer 224 views
1 answer 177 views
...