Skip to content

Instantly share code, notes, and snippets.

@MasterAlish
MasterAlish / multiple_choice_with_number.py
Last active October 1, 2015 23:05
Django Forms Filed for Multiple choice with number field for each choice
class CheckboxChoiceWithNumberInput(forms.widgets.CheckboxChoiceInput):
def render(self, name=None, value=None, attrs=None, choices=()):
if self.id_for_label:
label_for = format_html(u' for="{0}"', self.id_for_label)
else:
label_for = u''
return format_html(u'{1}<label{0}> {2}</label> <input type="number" value="0" name="{3}_number[{4}]"/>', label_for, self.tag(), self.choice_label, self.name, self.choice_value)
@MasterAlish
MasterAlish / letter_insert.js
Created September 15, 2015 16:28
JS insert letter into input at the selection
insert_letter: function (input_id, letter) {
var elem = document.getElementById(input_id);
var selectionStart = elem.selectionStart;
var selectionEnd = elem.selectionEnd;
var start = elem.value.substring(0, selectionStart);
var finish = elem.value.substring(selectionEnd, elem.value.length);
elem.value = start + letter + finish;
elem.focus();
elem.selectionStart = selectionStart + 1;
elem.selectionEnd = selectionStart + 1;
@MasterAlish
MasterAlish / paginate_queryset.py
Last active August 29, 2015 14:18
Paginate every models list page
# define in settings.py to able to use request in templates
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
)
# in templatetags
@register.inclusion_tag("gist:pagination_pages.html")
@MasterAlish
MasterAlish / pagination.py
Created April 10, 2015 10:16
Pagination python
# total - total pages count
# page - current page
# neighbour_count - nighbour pages count, from each side
#
# 'first' - link to first page
# 'last' - link to last page
def pagination(total, page=1, neighbour_count=2):
result = []
if page-neighbour_count > 2:
@MasterAlish
MasterAlish / html_input_dict.py
Last active June 29, 2020 17:58
Django html input array and dict
def get_html_input_dict(self, query_dict, param):
dictionary = {}
regex = re.compile('%s\[([\w\d_]+)\]' % param)
for key, value in query_dict.items():
match = regex.match(key)
if match:
inner_key = match.group(1)
dictionary[inner_key] = value
return dictionary
@MasterAlish
MasterAlish / olymp.krsu.147
Created January 4, 2014 10:38
olymp.krsu.147
import java.util.Scanner;
public class Solution {
public static void main(String[] arg){
int N, M;
int[] A;
Scanner in = new Scanner(System.in);
N = in.nextInt();
M = in.nextInt();
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
@MasterAlish
MasterAlish / gist:7226122
Created October 30, 2013 02:06
olymp.krsu.248
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
private final double y2;
private final double y1;
private final double x2;
@MasterAlish
MasterAlish / rectangularGrid.rb
Created August 22, 2013 19:06
Solution for "sunloverz / gist:6309925" on ruby
class RectangularGrid
def countRectangles(n,m)
sum = 0
for i_n in 1..n
for i_m in 1..m
if i_m != i_n
sum = sum + (m-i_m+1)*(n-i_n+1)
end
end
end
@MasterAlish
MasterAlish / rectangularGrid.cpp
Created August 22, 2013 19:03
Solution for "sunloverz / gist:6309925" on c++
#include <stdio.h>
class RectangularGrid{
public:
long long countRectangles(int n, int m){
long long sum=0;
for(int i_n=1; i_n<=n; i_n++)
for(int i_m=1; i_m<=m; i_m++)
if(i_m!=i_n)
sum+=(m-i_m+1)*(n-i_n+1);