0% found this document useful (0 votes)
39 views

NOI Notes

This document provides code snippets and explanations for common Python programming tasks and C++ syntax examples. The Python section includes one-liners for formatting strings and numbers, manipulating lists by removing/reordering elements, reversing strings, and converting between number bases. The C++ sections give multi-line code examples for sorting vectors of integers and checking if a modified vector is in non-decreasing order, with explanations of the algorithms. Standard C++ libraries and namespaces are imported, and vectors are used to store integer inputs and pass them to sorting/checking functions.

Uploaded by

Ryan Shaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

NOI Notes

This document provides code snippets and explanations for common Python programming tasks and C++ syntax examples. The Python section includes one-liners for formatting strings and numbers, manipulating lists by removing/reordering elements, reversing strings, and converting between number bases. The C++ sections give multi-line code examples for sorting vectors of integers and checking if a modified vector is in non-decreasing order, with explanations of the algorithms. Standard C++ libraries and namespaces are imported, and vectors are used to store integer inputs and pass them to sorting/checking functions.

Uploaded by

Ryan Shaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python

Put 0s in front a number in strings:


- ~string~.zfill(digit)

Example:
num = “50”
print(num.zfill(5))
#00050

Put trailing 0s behind a float:


- format(~float~, ‘.digitf’)

Example:
num = 2.5
ans = format(num, ‘.3f’)
print(ans)
#2.500

Remove an element in a list:


- ~list~.pop(digit)

Example:
num_list = [69, 420, 911]
num_list.pop(1)
print(num_list)
#[69, 911]

Sort list in reverse:


- ~list~.sort(reverse = True)

Example:
nums = [4, 67, 1, 2, 9]
nums.sort(reverse = True)
print(nums)
#[67, 9, 4, 2, 1]

Move first index in list to the back:


- ~list~ = ~list~[1:] + ~list~[:1]

Example:
alphabet = [“a”, “b”, “c”]
alphabet = alphabet[1:] + alphabet[:1]
print(alphabet)
#[“b”, “c”, “a”]
Move last index in list to the front:
- ~list~ = ~list~[:1] + ~list~[1:]

Example:
alphabet = [“a”, “b”, “c”]
alphabet = alphabet[-1:] + alphabet[:-1]
print(alphabet)
#[“c”, “a”, “b”]
Reverse a string:
- ~string~[::-1]

Example:
hello = “Hello World”[::-1]
print(hello)
#dlroW olleH

Get value of pi:


- math.pi is a constant float for pi.
import math
math.pi

Convert an integer into binary:


- bin(~integer~).replace(“0b”, “”)

Example:
final = bin(20).replace(“0b”, “”).zfill(8)
print(final)
#00010100

Convert an integer into hexadecimal:


- hex(~integer~).replace(“0x”, “”)

Example:
final = hex(2553918).replace(“0x”, “”)
print(final)
#26f83e
C++

Syntax:

--------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main(){
long long N, A, jar_num = 0, count = 0, total = 0;
vector <long long> nuts;
vector <long long> nuts_space;
vector <long long> nuts_space_arranged;
cin>>N;
for (long long izz = 0; izz<N; izz++){
cin>>A;
nuts.push_back(A);
}
for (long long ick = 0; ick<N; ick++){
cin>>A;
nuts_space.push_back(A);
}
nuts_space_arranged = nuts_space;
sort(nuts_space_arranged.begin(), nuts_space_arranged.end(), greater <long long>());
long long total_nuts = 0;
for (long long ill = 0; ill<N; ill++){
total_nuts += nuts[ill];
}
for (long long cde = 0; cde<N; cde++){
total_nuts -= nuts_space_arranged[cde];
jar_num++;
if (total_nuts <= 0){
break;
}
}
cout<<jar_num<<" ";
for (long long i = 0; i < jar_num; i++){
for (long long abc = 0; abc < nuts_space.size(); abc++){
if (nuts_space_arranged[i] == nuts_space[abc]){
abc = nuts_space.size() + 1;
}
count++;
}
nuts.erase(nuts.begin()+count);
nuts_space.erase(nuts_space.begin()+count);
count = 0;
}
for (long long abcde = 0; abcde<nuts.size(); abcde++){
total += nuts[abcde];
}
cout<<total;
return 0;
}

--------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main(){
int N, K, A, count = 0;
bool flag = true;
vector <int> numbers;
vector <int> numbers_edited;
vector <int> arranged;
cin>>N>> K;
for (int i = 0; i<N; i++){
cin>>A;
numbers.push_back(A);
arranged.push_back(A);
numbers_edited.push_back(A);
}
sort(arranged.begin(), arranged.end());
for (int i = 0; i<numbers.size(); i++){
for (int abc = i; abc<numbers.size(); abc++){
for (int c = i; c<abc; c++){
for (int cba = 0; cba < numbers_edited.size(); cba++){
if (c == numbers_edited[cba]){

numbers_edited.erase(numbers_edited.begin()+cba);
}
}
numbers_edited = numbers;
}
for (int cba = 1; cba<numbers_edited.size()-1; cba++){
if (numbers_edited[cba]<numbers_edited[cba-1]){
flag = false;
break;
}
}
if (flag ==true){
count++;
}
}
}
cout<<count;
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
--------------------------------------------------------------------------------------------------------------------------------------------

You might also like