Git Product home page Git Product logo

one-time-projects's Introduction

python-some-lang-tips

some language tips

Swap to variables

a = a + b
b = a - b
a = a - b

Отсортировать список по последней букве второго элемента

items = [('one', 'two'), ('three', 'four'), ('five', 'six'), ('string', 'a')]
sorted_items = sorted(items, key=lambda x: x[-1][-1])  
# sorted_items = [ ('string', 'a'), ('one', 'two'), ('three', 'four'), ('five', 'six')]

Обратные индексы. Вывести все элементы кортежа countries, кроме двух последних и трех первых.

countries = ('Russia', 'Argentina', 'Slovakia', 'Canada', 'Slovenia', 'Italy', 'Spain', 'Ukraine', 'Chile', 'Cameroon')
print(countries[3:-2])
# ('Canada', 'Slovenia', 'Italy', 'Spain', 'Ukraine')

CSV file reader

import csv

cnt = 0
with open("crimes.csv") as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

Fibonacci using lambda

fib = lambda x : 1 if x <= 2 else fib(x - 1) + fib(x - 2)
print(fib(31))

Заполнение матрицы спиралью

rows, cols = map(int, input().split())

matrix = [[0] * cols for _ in range(rows)]

#  0,  1  right
#  1,  0  down
#  0, -1  left
# -1,  0  up
by_row, by_col = 0, 1
r, c = 0, 0

for num in range(1, rows * cols + 1):
    matrix[r][c] = num
    # change direction
    if matrix[(r + by_row) % rows][(c + by_col) % cols]:
        by_row, by_col = by_col, -by_row 
    # move by direction
    r += by_row
    c += by_col
    
[print(*[str(el).ljust(3) for el in row]) for row in matrix]

one-time-projects's People

Contributors

dark-tulip avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.