Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, February 5, 2024

Algorithm to Shift last two elements of a python list and pad in between

 In this scenario, I got a list that most always contain six items. If the items contained are less that six, always move the last two items to the end and pad between with empty space.

For example this ['A', 'B', 'E', 'F'] list will becomes this: ['A', 'B', '', '', 'E', 'F']

The code:

# Shift last two elements of a list and pad in between with a '<empty string>'
input_list = ['A', 'B', 'E', 'F']
print('Original list: ', input_list)

last_two_elem = input_list[-2:] # Get last two elements
del input_list[-2:] # Delete last two elements
print(f'Last two elements of the list: {last_two_elem}, the lsit after removing last two elements: {input_list}')

# Pad the list to nth time...
pad_value = 4
input_list += [''] * (pad_value - len(input_list)) # for 6 len list...
print(f'The lsit after padding: {input_list}')

# Extend the list with the last two elements...
input_list.extend(last_two_elem)
print(f'The lsit after extending: {input_list}')



That is it!

Monday, March 7, 2016

70+ Free Python Programming Language Learning Resources

Hello,

Today, I present to you over 70 useful materials for learning the worlds dynamic and strongly typed programming language that is designed to emphasize usability, Python! The list contains two similar but incompatible versions of Python in widespread use (2 and 3).

2)    A Guide to Python's Magic Methods - Rafe Kettler
3)    Automate the Boring Stuff - Al Sweigart
4)    Biopython (PDF)
6)    Building Skills in Python (PDF) (2.6)
7)    Byte of Python (2.7.x)
10) Composing Programs (3.x)
12) Dive into Python - Mark Pilgrim (2.3)
13) Dive into Python 3 - Mark Pilgrim (3.0)
15) Google's Python Class (2.4 - 2.x)
17) Hacking Secret Cyphers with Python - Al Sweigart (3.3)
20) How to Think Like a Computer Scientist: Learning with Python - Allen B. Downey, Jeff Elkner and Chris Meyers (2.4)
21) Intermediate Python - Muhammad Yasoob Ullah Khalid (1st edition)
22) Introduction to Programming Using Python - Cody Jackson (1st edition) (2.3)
24) Introduction to python - Kracekumar (2.7.3)
28) Learn Python The Hard Way (2.5 - 2.6)
29) Learn to Program Using Python - Cody Jackson (PDF)
31) Learning to Program with Python - Richard L. Halterman (PDF) (3.2)
33) Making Games with Python & Pygame - Al Sweigart (2.7)
34) Modeling Creativity: Case Studies in Python - Tom D. De Smedt (PDF)
38) Porting to Python 3: An In-Depth Guide (2.6 - 2.x & 3.1 - 3.x)
39) Practical Programming in Python - Jeffrey Elkner (PDF)
40) Problem Solving with Algorithms and Data Structure using Python - Bradley N. Miller and David L. Ranum