Skip to content

Instantly share code, notes, and snippets.

View mpampols's full-sized avatar

Marc Pàmpols mpampols

View GitHub Profile
@mpampols
mpampols / alphabet-soup.py
Created January 24, 2012 15:46
Facebook Hacker Cup 2012 Qualification Round - Alphabet Soup
#!/usr/bin/env python
"""
Alfredo Spaghetti really likes soup, especially when it contains alphabet pasta. Every day he constructs a sentence from letters, places the letters into a bowl of broth and enjoys delicious alphabet soup.
Today, after constructing the sentence, Alfredo remembered that the Facebook Hacker Cup starts today! Thus, he decided to construct the phrase "HACKERCUP". As he already added the letters to the broth, he is stuck with the letters he originally selected. Help Alfredo determine how many times he can place the word "HACKERCUP" side-by-side using the letters in his soup.
Input
The first line of the input file contains a single integer T: the number of test cases. T lines follow, each representing a single test case with a sequence of upper-case letters and spaces: the original sentence Alfredo constructed.
@mpampols
mpampols / billboards.py
Created January 24, 2012 16:15
Facebook Hacker Cup 2012 Qualification Round - Billboards
#!/usr/bin/env python
"""
We are starting preparations for Hacker Cup 2013 really early. Our first step is to prepare billboards to advertise the contest. We have text for hundreds of billboards, but we need your help to design them.
The billboards are of different sizes, but are all rectangular. The billboard widths and heights are all integers. We will supply you with the size in inches and the text we want printed. We want you to tell us how large we can print the text, such that it fits on the billboard without splitting any words across lines. Since this is to attract hackers like yourself, we will use a monospace font, meaning that all characters are of the same width (e.g.. 'l' and 'm' take up the same horizontal space, as do space characters). The characters in our font are of equal width and height, and there will be no additional spacing between adjacent characters or adjacent rows. If you print a word on one line and print the next word on the next line, you do not need to print a space between
@mpampols
mpampols / validators.py
Created February 2, 2012 16:18
Plone 4.x field validator: Validate unique title
from zope.component import adapts
from Products.Archetypes.interfaces import IObjectPostValidation
from Products.PythonScripts.standard import html_quote
from Products.CMFCore.utils import getToolByName
from Products.statusmessages.interfaces import IStatusMessage
from aeau.contenttypes import contenttypesMessageFactory as _
from aeau.contenttypes.interfaces import ISoci
from Acquisition import aq_inner
from Acquisition import aq_parent
@mpampols
mpampols / csv_import.py
Created March 30, 2012 22:41
Import CSV data to Plone content type
# -*- coding: utf-8 -*-
import datetime
import urllib
from Acquisition import aq_inner
from zope import interface
from zope import schema
from zope.app.pagetemplate import viewpagetemplatefile
@mpampols
mpampols / add_subscriber.py
Created March 31, 2012 14:32
Add / Remove Plone object subscribers and send a notification email on state change
# Add a subscriber email to Plone object with Subscribers field
def add_subscriber(self,plone_object,email):
from Products.PythonScripts.standard import html_quote
from Products.CMFCore.utils import getToolByName
plone_utils = getToolByName(self, 'plone_utils', None)
workflowTool = getToolByName(self, "portal_workflow")
@mpampols
mpampols / duplicarCarpeta.py
Created February 27, 2013 08:13
Copy the two files inside /custom folder of your Plone instance and run "/duplicarCarpeta" inside the context of the folder you want to translate. The second script will run and copy all the original contents to the linked translated folders and will set the setLanguage to the destination language.
request = container.REQUEST
idiomactual = context.REQUEST.other['LANGUAGE']
from Products.CMFCore.utils import getToolByName
urltool = getToolByName(context, 'portal_url')
if context.restrictedTraverse('@@plone').isStructuralFolder():
path_to_translate = '/'.join(context.getPhysicalPath())
else:
#!/bin/sh
# This script deletes an entire Django database, calls syncdb, and imports data tables
# Drop all tables in cascade
psql tablename << EOF
DROP SCHEMA public CASCADE;
CREATE SCHEMA "public" AUTHORIZATION "postgres";
EOF
# Syncdb
@mpampols
mpampols / imageevent.py
Created February 4, 2014 11:24
This is a sample extended content type for Plone that uses the Event Schema adding some new fields (Image, Categories and Location). Categories field is using a NamedVocabulariy and Location field uses a TTGoogleMapMarker
"""Definition of the imageevent content type
"""
from zope.interface import implements
from Products.Archetypes import atapi
from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import schemata
# -*- Message Factory Imported Here -*-
@mpampols
mpampols / banner.py
Created February 4, 2014 11:35
A Plone content type used for baners with a string URL and a Reference Field to any other content type
"""Definition of the banner content type
"""
from zope.interface import implements
from Products.Archetypes import atapi
from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import schemata
# -*- Message Factory Imported Here -*-
@mpampols
mpampols / minisite.py
Created February 4, 2014 11:42
A Plone content type that creates a "minisite", this also creates new folders and contents to this new folderish content.
# -*- coding: utf-8 -*-
"""Definition of the minisite content type
"""
from zope.interface import implements
from Products.Archetypes import atapi
from Products.ATContentTypes.content import folder
from Products.ATContentTypes.content import schemata