Skip to content

Instantly share code, notes, and snippets.

@mailarchis
mailarchis / 3sum.py
Created January 8, 2011 16:06
3Sum Problem
"""
Problem Statement
Given a set of numbers M and another number k
Find if there exists 3 numbers a,b,c in M such that
a+b+c = k
in complexity O(N^2)
Solution
This code solves the problem in O(N^2). However, following assumptions had been made
Set comprises of positive integers.
Note - the code can be customized to handle negative numbers and decimals
<table>
<tbody>
<tr>
<td>{{ first_name }}</td><td>{{ middle_name}}</td><td>{{ last_name }}</td>
</tr>
</tbody>
</table>
class NameWidget(forms.MultiWidget):
"""
Custom widget for Custom Field Name Field
"""
def __init__(self):
widgets = (
forms.TextInput(attrs={}),
forms.TextInput(attrs={}),
forms.TextInput(attrs={})
)
class AddressField(forms.MultiValueField):
"""
Custom field to take user inputs of Address
"""
def __init__(self, country_choices, city_choices, locality_choices, attrs=None):
self.widget = AddressWidget(country_choices,city_choices,locality_choices)
fields = (forms.CharField(label=_('Address Line 1'), max_length=100), forms.CharField(label=_('Address Line 2'), max_length=100), forms.ChoiceField(label=_('Country'), choices=country_choices), forms.ChoiceField(label=_('City'), choices=city_choices), forms.ChoiceField(label=_('Locality'), choices=locality_choices))
super(AddressField, self).__init__(fields, required=False)
class AddressWidget(forms.MultiWidget):
"""
Custom widget for Custom Field AddressField
"""
def __init__(self, country_choices, city_choices, locality_choices, attrs=None):
widgets = (forms.TextInput(), forms.TextInput(), forms.Select(attrs=attrs, choices=country_choices), forms.Select(attrs=attrs, choices=city_choices),forms.Select(attrs=attrs, choices=locality_choices), forms.TextInput())
super(AddressWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value: