ameykudari05's blog

By ameykudari05, history, 5 years ago, In English

Hi! I found python to be very easy to learn but I was not able to use it for cp before because of things like io speed which I did not know how to optimize earlier these are some things I learnt which made it much easier for me to use python for cp

Fast Input:

import sys
inp = [a.strip() for a in sys.stdin.readlines()]  #inp is an array of input lines.

Reading an array

arr = [int(x) for x in inp[i].split()]  # use float(x) if dealing with decimal values

Making 2d matrix ( or any n dim matrix ) of 0s

mat = [[0 for i in range(n)] for j in range(n)]

Printing without the \n:

print("output",end="")

Sorting using key (see example below)

arr = [[3,4],[1,2],[5,3],[-1,4]]
def fkey(a):
    return a[0]*a[1]
arr.sort(key=fkey)

Reversing array/string

arr=arr[::-1]

Note 1: if arr is a list,

brr=arr
brr[0]*=2 # this also modifies arr and both lists are an array of pointers to the same thing

To make a copy of an array use this code brr=list(arr) Copy of string is also similar brr=string(arr)

Note 2:

a="ilu python"
a[0]='a' # this line gives the following error:
#'str' object does not support item assignment

# so if you have to modify the string, convert it into a list first
( list(a) )

Note 3: division in python normally returns a float, so to get integer division similar to cpp use '//' (a//b) operator or int(a/b)

Cpp data structures and their python equivalents:

- vector : list
- Map : dict
- set : set
- string : str
- stack/queue: #(Implemented using list) https://www.geeksforgeeks.org/using-list-stack-queues-python/
- list : -

Useful Libraries

1. Counter (to import: `from collections import Counter` ) # to count number of occurances of each element in a list
2. itertools (import itertools) (https://docs.python.org/2/library/itertools.html#module-itertools)
3. math

Thanks!

  • Vote: I like it
  • +6
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by ameykudari05 (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by ameykudari05 (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by ameykudari05 (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

This is really useful. THANKS

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ameykudari05 (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

The fact that you need to convert string->list to modify it or watch out not to assign by reference (btw the way you're doing it is wrong, it's arr.copy()) should tell you Python isn't very good for CP. You can use it, but many operations are slow and should be avoided, including too much list manipulation.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks! I did not know about arr.copy(). And I agree python is not very good for cp and I myself use cpp most of the time but sometimes for div 2, A/B and sometimes C, its easier to code in python than cpp..

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Maybe useful link