Python moves the elements of a list one bit forward
- 2020-04-02 14:05:58
- OfStack
The problem
Define an int one-dimensional array with 10 elements, assign values of 1 to 10, and move the elements of the array forward by one position.
That is, a [0] = [1], a [1] = [2], a... The value of the last element is the value of the original first element, and then outputs the array.
To solve (Python)
#!/usr/bin/env python
#coding:utf-8
def ahead_one():
a = [i for i in range(10)]
b = a.pop(0)
a.append(b)
return a
if __name__ =="__main__":
print ahead_one()
To solve (racket 5.2.1)
#lang racket
; Define a function ahead-one
; Enter as a list of integers int-list Suppose its length is N
; The output is a list of integers of the same length whose order N The element of bit is int-list The first 1 Bit elements,
; its 1~N-1 The element of bit is int-list The first 2~N An element of
(define (ahead-one int-list)
(append (rest int-list) (list (first int-list))))
; Function call that should be output during normal operation '(2 3 4 5 6 7 8 9 10 1)
(ahead-one (list 1 2 3 4 5 6 7 8 9 10))