<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Omnia sunt communia &#187; Programação</title>
	<atom:link href="http://www.blog.amrlima.info/archives/category/programacao/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.amrlima.info</link>
	<description>gnu/linux, cultura livre e outras divagações</description>
	<lastBuildDate>Fri, 20 Jan 2012 20:18:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Paradoxo de São Petersburgo: um teste</title>
		<link>http://www.blog.amrlima.info/archives/1137</link>
		<comments>http://www.blog.amrlima.info/archives/1137#comments</comments>
		<pubDate>Sun, 08 May 2011 15:52:49 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[paradoxo são petersburgo]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=1137</guid>
		<description><![CDATA[Estava a ler estatística quando me deparei com este interessante paradoxo.
Este paradoxo, terá sido pela primeira vez publicado por Daniel Bernoulli(1700-1782) (se bem que tenha sido introduzido por Nicolas Bernoulli a Montmort em 1713) Supõe-se que dois indivíduos Pedro e Paulo concordam em jogar um jogo baseado em lançar uma moeda. Neste jogo, lança-se sucessivamente [...]]]></description>
			<content:encoded><![CDATA[<p>Estava a ler estatística quando me deparei com este interessante paradoxo.</p>
<blockquote><p>Este paradoxo, terá sido pela primeira vez publicado por Daniel Bernoulli(1700-1782) (se bem que tenha sido introduzido por Nicolas Bernoulli a Montmort em 1713) Supõe-se que dois indivíduos Pedro e Paulo concordam em jogar um jogo baseado em lançar uma moeda. Neste jogo, lança-se sucessivamente a moeda até dar cara, então, se a moeda tiver aparecido à n-ésima tentativa, Paulo dará a Pedro 2n-1 moedas. A questão que se coloca é sobre quanto deve Pedro pagar a Paulo pelo privilégio de jogar tal jogo?</p>
<p>Intuitivamente, talvez se indicasse uma qualquer quantia finita, mas na verdade, seja qual for a quantia que Pedro invista por cada jogo, se ele tiver dinheiro suficiente para jogar número suficiente de vezes ele deverá sair a lucrar. Em cada jogo ele tem œ de probabilidade de ganhar uma moeda, (œ)2 de ganhar duas moedas e assim sucessivamente, tendo portanto (œ)n de probabilidade de ganhar 2n-1 moedas. Como probabilisticamente é suposto por cada n jogos, ganhar (œ).1+(œ)2.2+&#8230;+(œ)n.2n-1 moedas e este somatório, quando n tende para infinito, tende também para infinito, conclui-se que se jogar número suficiente de jogos, poderá ganhar uma quantia de dinheiro superior a qualquer valor estabelecido. <a href="http://www.educ.fc.ul.pt/icm/icm2003/icm25/outrosparadoxosd.htm" target="_blank">Fonte</a></p></blockquote>
<p>Como estava aborrecido decidir por isto em código <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (Python).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> throw_coin<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'Head is 0, Tail is 1'</span><span style="color: #483d8b;">''</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
&nbsp;
  games = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Number of games: '</span><span style="color: black;">&#41;</span>
  payment = <span style="color: #ff4500;">100</span> <span style="color: #808080; font-style: italic;"># This is to define the money the player has to pay to be hable</span>
                <span style="color: #808080; font-style: italic;"># to play the game</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Initial player payment is %d&quot;</span> <span style="color: #66cc66;">%</span> payment
  cash = <span style="color: #ff4500;">0</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span> <span style="color: black;">&#40;</span>games<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    throws = <span style="color: #ff4500;">0</span>
    throws = throws + <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span>throw_coin<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># throw coin until head comes up</span>
      throws = throws + <span style="color: #ff4500;">1</span>
&nbsp;
    cash = cash + <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">**</span> <span style="color: black;">&#40;</span>throws - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># Equivalent to  2^(i-1)</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Throws %d&quot;</span> <span style="color: #66cc66;">%</span> throws
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You have wone: %d euros so far!&quot;</span> <span style="color: #66cc66;">%</span> cash
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You won %d €! You payed %d in advance. You result was %d €!&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>cash
  , payment, cash - payment<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
  main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/1137/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gerador de chaves aleatórias do euromilhões</title>
		<link>http://www.blog.amrlima.info/archives/1110</link>
		<comments>http://www.blog.amrlima.info/archives/1110#comments</comments>
		<pubDate>Thu, 07 Apr 2011 21:42:36 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[euromilhões]]></category>
		<category><![CDATA[gerador de chaves]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=1110</guid>
		<description><![CDATA[Cá está ele: http://www.euromillions.amrlima.info/
É bastante provável que haja alguns erros básicos, dada a minha noobice em JavaScript. Foi feito com meia hora de leitura.
O CSS e o restante grafismo demoraram muito mais a fazer do que a programação propriamente dita.
É quase inútil, é certo, mas vai ficar online para ver o número de visitas que [...]]]></description>
			<content:encoded><![CDATA[<p>Cá está ele: <a href="http://www.euromillions.amrlima.info/">http://www.euromillions.amrlima.info/</a></p>
<p>É bastante provável que haja alguns erros básicos, dada a minha noobice em JavaScript. Foi feito com meia hora de leitura.</p>
<p>O CSS e o restante grafismo demoraram muito mais a fazer do que a programação propriamente dita.</p>
<p>É quase inútil, é certo, mas vai ficar online para ver o número de visitas que uma coisa destas pode gerar, já que quase não ocupa espaço no servidor.</p>
<p>Se não quiserem pensar em números para jogar já sabem <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Ah, todo o código, (HTML, CSS, JavaScript) são <a href="http://www.gnu.org/licenses/agpl.html">AGPL</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/1110/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Verificador de chaves do euromilhões &#8211; v. 0.2</title>
		<link>http://www.blog.amrlima.info/archives/1087</link>
		<comments>http://www.blog.amrlima.info/archives/1087#comments</comments>
		<pubDate>Wed, 23 Mar 2011 01:34:10 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[euromilhões]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=1087</guid>
		<description><![CDATA[Na sequência do meu último post aqui no estaminé, venho colocar mais uma versão do verificador de chaves do euromilhões.
O meu colega António Dias enviou-me algumas alterações ao código tornando-o um pouco mais &#8220;Pythonico&#8221; e hoje adicionei validação de chaves e criação automática do ficheiro de chaves do euromilhões, caso este não exista.
Se houver tempo [...]]]></description>
			<content:encoded><![CDATA[<p>Na sequência do meu<a href="http://www.blog.amrlima.info/archives/1080"> último post</a> aqui no estaminé, venho colocar mais uma versão do verificador de chaves do euromilhões.</p>
<p>O meu colega António Dias enviou-me algumas alterações ao código tornando-o um pouco mais &#8220;Pythonico&#8221; e hoje adicionei validação de chaves e criação automática do ficheiro de chaves do euromilhões, caso este não exista.</p>
<p>Se houver tempo (e vontade) hei-de melhorar um pouco o programa, talvez adicionando suporte a argumentos de linha de comando.</p>
<p>Quem quiser, pode tirar o código <a href="http://dl.dropbox.com/u/281158/blogfiles/euromillions.py">aqui</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/1087/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Verificador de chaves do euromilhões</title>
		<link>http://www.blog.amrlima.info/archives/1080</link>
		<comments>http://www.blog.amrlima.info/archives/1080#comments</comments>
		<pubDate>Mon, 21 Mar 2011 00:12:26 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[euromilhões]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=1080</guid>
		<description><![CDATA[Tenho andado com vontade recomeçar a brincar com Python (a linguagem de programação, não o animal), muito devido aos posts do meu colega António Dias no Moodle que me têm despertado a curiosidade principalmente sobre programação orientada a objectos com esta linguagem.
Como sou um bocado preguiçoso e uma das coisas que me chateia é ter [...]]]></description>
			<content:encoded><![CDATA[<p>Tenho andado com vontade recomeçar a brincar com Python (a linguagem de programação, não o animal), muito devido aos posts do meu colega <a href="http://maracuja.homeip.net/">António Dias</a> no Moodle que me têm despertado a curiosidade principalmente sobre programação orientada a objectos com esta linguagem.</p>
<p>Como sou um bocado preguiçoso e uma das coisas que me chateia é ter de ver todas as semanas, uma a uma, as chaves do euromilhões para ver se fiquei milionário, fiz um pequeno programa para linha de comando, utilizando classes, para poupar tempo. Claro que a coisa fazia-se com &#8220;meia dúzia&#8221; de linhas de código sem usar classes, mas não era a mesma coisa <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Aqui fica o dito para os preguiçosos como eu. Precisam de ter um ficheiro chamado chaves.txt na mesma pasta que o programa com as suas chaves do euromilhões, uma em cada linha com os números separados por espaços e as estrelas no fim. Assim:</p>
<p>1 2 3 4 5 1 2 # os dois últimos números são as estrelas.</p>
<p>Depois o programa pede a chave desta semana e imprime o resultado para cada chave.</p>
<p>Claro que isto podia estar muito melhor com argumentos mas o sono chegou <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Ah, e não me responsabilizo se alguém deitar fora o papelinho que o faria milionário por causa de algum bug nisto <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> !</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#/usr/bin/env/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Copyright (C) 2011 António Lima</span>
<span style="color: #808080; font-style: italic;">#amrlima AT gmail.com</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This program is free software: you can</span>
<span style="color: #808080; font-style: italic;"># redistribute it and/or modify it under the terms of the GNU General Public</span>
<span style="color: #808080; font-style: italic;"># License as published by the Free Software Foundation, version 2.</span>
<span style="color: #808080; font-style: italic;"># A copy of this license can be obtained at http://www.gnu.org/copyleft/gpl.html</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> GameKey<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;A euromillion key&quot;&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>, stars = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>._numbers = key
    <span style="color: #008000;">self</span>._stars = stars
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> get_numbers<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Get bet numbers&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._numbers
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> get_stars<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Get bet stars&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._stars
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> set_numbers<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numbers<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>._numbers = numbers
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> set_stars<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, stars<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>._stars = stars
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> read<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Reads bet from terminal&quot;&quot;&quot;</span>
    line = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Type this weeks key separating each mumber with a space.'</span>\
    <span style="color: #483d8b;">' Two Stars last<span style="color: #000099; font-weight: bold;">\n</span>&amp;gt;&amp;gt;&amp;gt; '</span><span style="color: black;">&#41;</span>
    numbers = line.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#cast input values to int and push then to list</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> numbers:
      <span style="color: #008000;">self</span>._numbers.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._numbers<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">7</span>:
      <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Not enought values read'</span><span style="color: black;">&#41;</span> 
&nbsp;
    <span style="color: #008000;">self</span>._stars = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># pop stars from regular numbers to stars list</span>
    <span style="color: #008000;">self</span>._stars.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._numbers.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>._stars.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._numbers.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">################################################################################</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Bet<span style="color: black;">&#40;</span>GameKey<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;A euromillion bet&quot;&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>, stars = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
    GameKey.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, stars<span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>._numbers_hit = <span style="color: #ff4500;">0</span>
    <span style="color: #008000;">self</span>._stars_hit = <span style="color: #ff4500;">0</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> __check_numbers<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numbers<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Check how many numbers where hit&quot;&quot;&quot;</span>
    <span style="color: #808080; font-style: italic;"># check for numbers</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>._numbers:
      <span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> numbers:
        <span style="color: #ff7700;font-weight:bold;">if</span> a == b:
          <span style="color: #008000;">self</span>._numbers_hit = <span style="color: #008000;">self</span>._numbers_hit + <span style="color: #ff4500;">1</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> __check_stars<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, stars<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Check how many stars where hit&quot;&quot;&quot;</span>
    <span style="color: #808080; font-style: italic;"># check for stars</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>._stars:
      <span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> stars:
        <span style="color: #ff7700;font-weight:bold;">if</span> a == b:
          <span style="color: #008000;">self</span>._stars_hit = <span style="color: #008000;">self</span>._stars_hit + <span style="color: #ff4500;">1</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> check_result_parts<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numbers, stars<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Check euromillion result. This version takes two lists as arguments&quot;&quot;&quot;</span>
    <span style="color: #008000;">self</span>.__check_numbers<span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.__check_stars<span style="color: black;">&#40;</span>stars<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> check_result<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, week_key<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Check euromillion result. This version takes a Bet object as argument&quot;&quot;&quot;</span>
    <span style="color: #008000;">self</span>.__check_numbers<span style="color: black;">&#40;</span>week_key.<span style="color: black;">get_numbers</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.__check_stars<span style="color: black;">&#40;</span>week_key.<span style="color: black;">get_stars</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> get_numbers_hit<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Get the number of correct numbers. Always call check_result() fist&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._numbers_hit
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> get_stars_hit<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Get the number of correct stars. Always call check_result() fist&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._stars_hit
&nbsp;
<span style="color: #808080; font-style: italic;">############################ Utility functions ################################</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> read_bets<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;Reads a serie of bets from a filename and creates Bet objects. Returns
  a list of Bet objects&quot;&quot;&quot;</span>
  bet_lines = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># File lines containing bets</span>
  bets = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># for Bet objects</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Open file and read bets</span>
  f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> f:
    bet_lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
  f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> bet_lines:
    numbers_str = b.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># tokenize input</span>
    numbers = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    stars = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> numbers_str:
      numbers.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span> <span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Move stars from regular numbers to stars list</span>
    stars.<span style="color: black;">append</span><span style="color: black;">&#40;</span>numbers.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    stars.<span style="color: black;">append</span><span style="color: black;">&#40;</span>numbers.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    new_bet = Bet<span style="color: black;">&#40;</span>numbers, stars<span style="color: black;">&#41;</span>
    bets.<span style="color: black;">append</span><span style="color: black;">&#40;</span>new_bet<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> bets
&nbsp;
<span style="color: #808080; font-style: italic;">################################## Main program ################################</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
&nbsp;
  week_key = GameKey<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;"># read user's keys from file</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    my_bets = read_bets<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;chaves.txt&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'chaves.txt not found! Create the file, write your keys there and '</span>\
    <span style="color: #483d8b;">'try again'</span>
    <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># read this week's key from keyboad. Exit on fail</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    week_key.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Invalid euromilion key size, please try again'</span>
    <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
  i = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> bet <span style="color: #ff7700;font-weight:bold;">in</span> my_bets:
    bet.<span style="color: black;">check_result</span><span style="color: black;">&#40;</span>week_key<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-----Bet #%d------<span style="color: #000099; font-weight: bold;">\n</span>%d numbers hit &quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>i, bet.<span style="color: black;">get_numbers_hit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%d stars hit&quot;</span> <span style="color: #66cc66;">%</span> bet.<span style="color: black;">get_stars_hit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    i += <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
	main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/1080/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>User&#8217;s browser and platform php class</title>
		<link>http://www.blog.amrlima.info/archives/988</link>
		<comments>http://www.blog.amrlima.info/archives/988#comments</comments>
		<pubDate>Sat, 07 Aug 2010 18:22:14 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[os]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[user agent]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=988</guid>
		<description><![CDATA[Because I&#8217;ve been working in a drupal website, I tought it would be handy to learn some php so I can make some scripts for it, although I&#8217;m still a long long way from making a working drupal module. For now I&#8217;m using php code input format when I want to include some script.
As parte [...]]]></description>
			<content:encoded><![CDATA[<p>Because I&#8217;ve been working in a drupal website, I tought it would be handy to learn some php so I can make some scripts for it, although I&#8217;m still a long long way from making a working drupal module. For now I&#8217;m using php code input format when I want to include some script.</p>
<p>As parte of my learning I made a class (which I&#8217;me really not using for anythin now) to detect user browser and platform. It uses HTTP_USER_AGENT to obtain the relevant information. Note that te code is mostly untested. I just tested Firefox and Chromium under Linux and Chrome and IE under Windows.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Agent<span style="color: #009900;">&#123;</span>
 	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_browser</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_os</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_browsers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firefox&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;msie&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;chrome&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;opera&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;safari&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;mozilla&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_OS</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;windows&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;win32&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;linux&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;machintosh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __Construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 		<span style="color: #666666; font-style: italic;">//Get user agent</span>
 		<span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 		<span style="color: #666666; font-style: italic;">//Lower string case for easier matches</span>
 		<span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 		<span style="color: #b1b100;">print</span> <span style="color: #000088;">$info</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 		<span style="color: #666666; font-style: italic;">//Search for browser</span>
 		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gt</span><span style="color: #339933;">;</span>_browsers <span style="color: #b1b100;">as</span> <span style="color: #000088;">$browser</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
 			 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #339933;">,</span> <span style="color: #000088;">$browser</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 			 	<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
 			 <span style="color: #009900;">&#125;</span>
&nbsp;
 			 <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
 			 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser <span style="color: #339933;">=</span> <span style="color: #000088;">$browser</span><span style="color: #339933;">;</span>
&nbsp;
 			 	<span style="color: #666666; font-style: italic;">//We need to change the name os IE from msie to IE</span>
 			 	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser <span style="color: #339933;">===</span> <span style="color: #0000ff;">&quot;msie&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Internet &quot;</span> <span style="color: #339933;">.</span>
 			 			<span style="color: #0000ff;">&quot;Explorer&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 			 	<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// We've found the browser, leave the loop</span>
 			 <span style="color: #009900;">&#125;</span>
 		<span style="color: #009900;">&#125;</span>
&nbsp;
 		<span style="color: #666666; font-style: italic;">//Search for OS</span>
 		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gt</span><span style="color: #339933;">;</span>_OS <span style="color: #b1b100;">as</span> <span style="color: #000088;">$os</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
 			 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #339933;">,</span> <span style="color: #000088;">$os</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
 			 	<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
 			 <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
 			 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os <span style="color: #339933;">=</span> <span style="color: #000088;">$os</span><span style="color: #339933;">;</span>
&nbsp;
 			 	<span style="color: #666666; font-style: italic;">//If windows identifies as win32 we change it to Windows</span>
 			 	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;win32&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Windows&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 			 	<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//We've found our OS, leave loop</span>
 			 <span style="color: #009900;">&#125;</span>
 		<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">//Make first letter of browser and OS uppercase</span>
 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser <span style="color: #339933;">=</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os <span style="color: #339933;">=</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 	<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">//Selector that returns browser type. The b in Browser is capitalized because of the</span>
 	<span style="color: #666666; font-style: italic;">//library funtion get_browser</span>
 	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">get_Browser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser<span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">//Selector returns os</span>
 	<span style="color: #000000; font-weight: bold;">function</span> get_os<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os<span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">//Browser modifier</span>
 	<span style="color: #000000; font-weight: bold;">function</span> set_browser<span style="color: #009900;">&#40;</span>string <span style="color: #000088;">$browser</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser <span style="color: #339933;">=</span> <span style="color: #000088;">$browser</span><span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">//Os modifier</span>
 	<span style="color: #000000; font-weight: bold;">function</span> set_os<span style="color: #009900;">&#40;</span>string <span style="color: #000088;">$os</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os <span style="color: #339933;">=</span> <span style="color: #000088;">$os</span><span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">//Print browser and OS</span>
 	<span style="color: #000000; font-weight: bold;">function</span> print_agent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 		<span style="color: #b1b100;">print</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_browser<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;@&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_os<span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span> 
&nbsp;
 <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/988/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another C mathematical program</title>
		<link>http://www.blog.amrlima.info/archives/869</link>
		<comments>http://www.blog.amrlima.info/archives/869#comments</comments>
		<pubDate>Fri, 15 Jan 2010 19:28:16 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=869</guid>
		<description><![CDATA[Disclaimer: For the non geeky programming lovers out there, sorry for spamming you with this bunch of incomprehensible lines of text!
The first time I wrote some kind of program was back in high school with a Ti80 calculator which had some sort of BASIC programming language. After reading the manual I spent hours making small [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: For the non geeky programming lovers out there, sorry for spamming you with this bunch of incomprehensible lines of text!</p>
<p>The first time I wrote some kind of program was back in high school with a <a href="http://www.albion.edu/math/MBollman/TI80.jpg" target="_blank">Ti80 calculator</a> which had some sort of BASIC programming language. After reading the manual I spent hours making small mathematical programs to speedup my problem solving study. I must say the programs were not that useful to math because in the tests I had of course to show all the calculation I did&#8230; Then I forgot most things about programming&#8230; Until some years ago <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Anyways, what does this have to do with the code below? Almost nothing! The code bellow calculates the <a href="http://en.wikipedia.org/wiki/Mode_(statistics)" target="_blank">mode</a> of n numbers provided by a user. It dynamically creates an n size array and checks with value is seen more often. I still haven&#8217;t done a python version this time, if someone wan&#8217;ts to do it in python or any other language, please do! <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Here it goes.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Create a n sized array*/</span>
<span style="color: #993333;">int</span><span style="color: #339933;">*</span> makearray<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span><span style="color: #339933;">*</span> v<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
    v <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">*</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Alocates n int numbers*/</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Array elements:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #808080; font-style: italic;">/*Fill the array with user input*/</span>
        scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> v<span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Get the size of an array*/</span>
<span style="color: #993333;">int</span> arraylen<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> v<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> i<span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">return</span> i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Count how many times a given element is repeated in an array*/</span>
<span style="color: #993333;">int</span> countelem<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> v<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> num<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> n<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> arraylen<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> num<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            n<span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> n<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> mode<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span><span style="color: #339933;">*</span> v<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Size of the array?<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/*We don't wan't anyone creating a oversized array: DANGER'*/</span>
    <span style="color: #808080; font-style: italic;">/*So we limit it's' size. Here I randomly defined 100*/</span>
    <span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>
        scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    v <span style="color: #339933;">=</span> makearray<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/*Update the wich value is repeated more ofted -&gt; Mode*/</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>countelem<span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> count<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            count <span style="color: #339933;">=</span> countelem<span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Update the value of the maximum number of repeats*/</span>
            <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mode <span style="color: #339933;">=</span> v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Update mode*/</span>
        <span style="color: #009900;">&#125;</span> 
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Mode: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> mode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>EDIT: There was a little bug with my code, it&#8217;s fixed now <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><a href="http://maracuja.homeip.net/">One of my colegues</a> made a python version <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Hope he doesn&#8217;t mind if I publish it <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/bin/sh python</span>
<span style="color: #808080; font-style: italic;">#-*- coding: utf-8 -*-</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> makearray<span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;Creates the dictionary with the elements input by the user
  &quot;&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Array elements (end with empty line):&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    s = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-&gt; &quot;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># get an element</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> s:
      <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      i = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> v.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:      <span style="color: #808080; font-style: italic;"># if already exists, increment frequency</span>
        v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:                 <span style="color: #808080; font-style: italic;"># if not, create it</span>
        v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:                 <span style="color: #808080; font-style: italic;"># error handling</span>
       <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;not an integer&quot;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">#-----------------------------------------------------------------------</span>
<span style="color: #808080; font-style: italic;"># program</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># instead of a vector or array, we do this with a dictionary</span>
<span style="color: #808080; font-style: italic;"># that way we can keep the frequency count with each entry</span>
v = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
makearray<span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># get the mode</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>:
  mode = <span style="color: #ff4500;">0</span>
  maxfreq = <span style="color: #ff4500;">0</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> v.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> maxfreq:
      mode = i
      maxfreq = v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Mode %d, with frequency %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>mode, maxfreq<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">else</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Empty array&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/869/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Prime factors code in C and Python</title>
		<link>http://www.blog.amrlima.info/archives/865</link>
		<comments>http://www.blog.amrlima.info/archives/865#comments</comments>
		<pubDate>Thu, 14 Jan 2010 23:07:11 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[factors]]></category>
		<category><![CDATA[Prime numbers]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=865</guid>
		<description><![CDATA[Today someone spoke about prime factors and I thought it was something interesting to code. I needed to practice my C so I&#8217;ve coded it in C. Here it goes:

#include &#60;stdio.h&#62;
#define VMAX 25
&#160;
int main&#40;&#41;
&#123;
    /*Prime numbers &#60; 100 */
    int primes&#91;VMAX&#93; = &#123;2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97&#125;;
    int factors&#91;VMAX&#93; = &#123;&#125;; [...]]]></description>
			<content:encoded><![CDATA[<p>Today someone spoke about <a href="http://en.wikipedia.org/wiki/Prime_factor">prime factors</a> and I thought it was something interesting to code. I needed to practice my C so I&#8217;ve coded it in C. Here it goes:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#define VMAX 25</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/*Prime numbers &lt; 100 */</span>
    <span style="color: #993333;">int</span> primes<span style="color: #009900;">&#91;</span>VMAX<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span><span style="color: #0000dd;">5</span><span style="color: #339933;">,</span><span style="color: #0000dd;">7</span><span style="color: #339933;">,</span><span style="color: #0000dd;">11</span><span style="color: #339933;">,</span><span style="color: #0000dd;">13</span><span style="color: #339933;">,</span><span style="color: #0000dd;">17</span><span style="color: #339933;">,</span><span style="color: #0000dd;">19</span><span style="color: #339933;">,</span><span style="color: #0000dd;">23</span><span style="color: #339933;">,</span><span style="color: #0000dd;">29</span><span style="color: #339933;">,</span><span style="color: #0000dd;">31</span><span style="color: #339933;">,</span><span style="color: #0000dd;">37</span><span style="color: #339933;">,</span><span style="color: #0000dd;">41</span><span style="color: #339933;">,</span><span style="color: #0000dd;">43</span><span style="color: #339933;">,</span><span style="color: #0000dd;">47</span><span style="color: #339933;">,</span><span style="color: #0000dd;">53</span><span style="color: #339933;">,</span><span style="color: #0000dd;">59</span><span style="color: #339933;">,</span><span style="color: #0000dd;">61</span><span style="color: #339933;">,</span><span style="color: #0000dd;">67</span><span style="color: #339933;">,</span><span style="color: #0000dd;">71</span><span style="color: #339933;">,</span><span style="color: #0000dd;">73</span><span style="color: #339933;">,</span><span style="color: #0000dd;">79</span><span style="color: #339933;">,</span><span style="color: #0000dd;">83</span><span style="color: #339933;">,</span><span style="color: #0000dd;">89</span><span style="color: #339933;">,</span><span style="color: #0000dd;">97</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> factors<span style="color: #009900;">&#91;</span>VMAX<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Prime factors*/</span>
    <span style="color: #993333;">int</span> repeat<span style="color: #009900;">&#91;</span>VMAX<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Store times each factor repeats*/</span>
    <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> n<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Type a number to find it's prime factors: <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/*Start to divide the number by prime numbers while a is not 1*/</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>VMAX<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">!=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">%</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
&nbsp;
            a <span style="color: #339933;">=</span> a <span style="color: #339933;">/</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            factors<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*The prime factor is the one being used*/</span>
            n<span style="color: #339933;">++;</span> <span style="color: #808080; font-style: italic;">/*Count how many times each prime is used*/</span>
            <span style="color: #009900;">&#125;</span> 
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">%</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> n <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> 
        repeat<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> n<span style="color: #339933;">;</span>
        n <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Reinitialize counter*/</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Prime factors:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>VMAX<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/*Print all factors in the array != 0*/</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>factors<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #808080; font-style: italic;">/*Since x ^ 0 = 1, we have to print x ^ 1 instead*/</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>repeat<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d ^ 1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> factors<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d ^ %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> factors<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> repeat<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>    
    <span style="color: #009900;">&#125;</span>       
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I really like python but unfortunately I haven&#8217;t had the time to code anything with python. Now it&#8217;s learning C  , then C++ and so on. But this time I wanted to code this in python also. I&#8217;m a bit out of practice and my brain was thinking in C. But here it goes.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env/python</span>
VMAX = <span style="color: #ff4500;">25</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Prime numbers &lt; 100</span>
primes = <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">7</span>,<span style="color: #ff4500;">11</span>,<span style="color: #ff4500;">13</span>,<span style="color: #ff4500;">17</span>,<span style="color: #ff4500;">19</span>,<span style="color: #ff4500;">23</span>,<span style="color: #ff4500;">29</span>,<span style="color: #ff4500;">31</span>,<span style="color: #ff4500;">37</span>,<span style="color: #ff4500;">41</span>,<span style="color: #ff4500;">43</span>,<span style="color: #ff4500;">47</span>,<span style="color: #ff4500;">53</span>,<span style="color: #ff4500;">59</span>,<span style="color: #ff4500;">61</span>,<span style="color: #ff4500;">67</span>,<span style="color: #ff4500;">71</span>,<span style="color: #ff4500;">73</span>,<span style="color: #ff4500;">79</span>,<span style="color: #ff4500;">83</span>,<span style="color: #ff4500;">89</span>,<span style="color: #ff4500;">97</span><span style="color: black;">&#93;</span>
factors = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>VMAX <span style="color: #808080; font-style: italic;">#Prime factors</span>
repeat = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>VMAX <span style="color: #808080; font-style: italic;">#Store times each factor repeats</span>
i = <span style="color: #ff4500;">0</span>
n = <span style="color: #ff4500;">0</span>
k = <span style="color: #ff4500;">0</span>
&nbsp;
a = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Type a number to find it's prime factors:&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Start to divide the number by prime numbers while a is not 1</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>VMAX<span style="color: black;">&#41;</span>: 
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>a <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> <span style="color: black;">&#40;</span><span style="color: #008000;">int</span> <span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> primes<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
&nbsp;
        a = <span style="color: #008000;">int</span> <span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span> / primes<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">print</span> primes<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
        factors<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = primes<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># The prime factor is the one being used</span>
&nbsp;
        n = n + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;">#*Count how many times each prime is used</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> n
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">int</span> <span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> primes<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        n = <span style="color: #ff4500;">0</span> <span style="color: #808080; font-style: italic;">#Reinitialize counter</span>
    repeat<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span> = n
    n = <span style="color: #ff4500;">0</span>
    k = k + <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Prime factors:&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Print all factors in the array != 0</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>VMAX<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>factors<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Since x ^ 0 = 1, we have to print x ^ 1 instead</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>repeat<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> factors<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s ^ %s &quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>factors<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, repeat<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/865/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Update</title>
		<link>http://www.blog.amrlima.info/archives/734</link>
		<comments>http://www.blog.amrlima.info/archives/734#comments</comments>
		<pubDate>Thu, 14 May 2009 14:16:41 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Debian]]></category>
		<category><![CDATA[gnome]]></category>
		<category><![CDATA[Pessoal]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[trabalho]]></category>
		<category><![CDATA[Traduções]]></category>
		<category><![CDATA[travian]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=734</guid>
		<description><![CDATA[Não tenho tido tempo para postar e muito menos para ler as minhas feeds. Ando apenas a ouvir alguns podcasts enquanto trabalho e é o que me mantém um pouco actualizado sobre as novidades do mundo FOSS. Pouco mais tenho feito do que trabalhar e uma formação em horário pós-laboral ocupa-me quase todo o tempo [...]]]></description>
			<content:encoded><![CDATA[<p>Não tenho tido tempo para postar e muito menos para ler as minhas feeds. Ando apenas a ouvir alguns podcasts enquanto trabalho e é o que me mantém um pouco actualizado sobre as novidades do mundo FOSS. Pouco mais tenho feito do que trabalhar e uma formação em horário pós-laboral ocupa-me quase todo o tempo livre. Finalmente acaba esta semana e poderei voltar a ter dias mais calmos.</p>
<p>O pouco tempo que tenho dedicado a contribuições tem sido preenchido a traduzir um módulo que está a dar-me cabo do juízo. gnome-disk-utility, tem cada string incompreensível e sem comentários o que torna aquilo intragável. Já traduzi coisas bem complicadas mas aquilo é demais. Parece-me que alguém vai reportar uns bugs um dia destes.</p>
<p>Finalmente tenho o meu desktop a bombar com Debian Squeeze/Sid. O portátil está com o Jaunty. Já tinha saudades de com um aptitude install -t unstable foo tenho um pacote do unstable e não tenho de esperar por um lançamento!</p>
<p>Junho vai ser um mẽs importante, vou dar um passo em que estou a pensar há mais de um ano. Provavelmente muita gente vai pensar que tenho uma veia de masoquista, mas <em>no pain, no gain. </em>Mais novidades sobre isso mais tarde.</p>
<p>Espero bloggar com mais frequência em breve! Para não dar em maluco pûs-me novamente a jogar travian, servidor pt8 amrlima, a aliança é WTF &#8211; What The Fonix. Não me ataquem por favor <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/734/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python scripts: Post song to identi.ca &amp; pidgin musictracker</title>
		<link>http://www.blog.amrlima.info/archives/641</link>
		<comments>http://www.blog.amrlima.info/archives/641#comments</comments>
		<pubDate>Wed, 11 Mar 2009 21:53:48 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[gnome]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[banshee]]></category>
		<category><![CDATA[dbus]]></category>
		<category><![CDATA[identica]]></category>
		<category><![CDATA[pidgin]]></category>
		<category><![CDATA[pythoon]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=641</guid>
		<description><![CDATA[Some days ago I wrote two python scripts: One that posts the current song beeing played by Banshee to identi.ca trought XMPP (using Pidgin) and a sort of musictracker for Pidgin also. Pidgin music tracker plugin crashes pidgin and it really does not work in Ubuntu Intrepid&#8230; or is it just me?
Both scripts are, well, [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago I wrote two python scripts: One that posts the current song beeing played by Banshee to identi.ca trought XMPP (using Pidgin) and a sort of <em>musictracker</em> for Pidgin also. Pidgin music tracker plugin crashes pidgin and it really does not work in Ubuntu Intrepid&#8230; or is it just me?</p>
<p>Both scripts are, well, pretty incomplete (thats being very nice to myself) and the code is not pretty, but I really didin&#8221;t have the time to improve them. Anyways, these where just to play around with python, dbus and to have fun! <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>These only work with Pidgin and Banshee, thought it&#8217;s pretty simple to make them work with other IM and Music players that have a dbus interface. I&#8217;ll post the scripts and instructions on how to use the, if there&#8217;s anyone crazy enought to use them <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><strong>Post song to identi.ca</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env/python</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Copyright (C) 2009 Antonio Lima</span>
<span style="color: #808080; font-style: italic;"># http://www.amrlima.info</span>
<span style="color: #808080; font-style: italic;"># This program is free software; you can redistribute it and/or</span>
<span style="color: #808080; font-style: italic;"># modify it under the terms of the GNU General Public License</span>
<span style="color: #808080; font-style: italic;"># as published by the Free Software Foundation; either version 2</span>
<span style="color: #808080; font-style: italic;"># of the License, or any later version.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This program is distributed in the hope that it will be useful,</span>
<span style="color: #808080; font-style: italic;"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span style="color: #808080; font-style: italic;"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span style="color: #808080; font-style: italic;"># GNU General Public License for more details.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> dbus
bus = dbus.<span style="color: black;">SessionBus</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
banshee = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.bansheeproject.Banshee&quot;</span>, <span style="color: #483d8b;">&quot;/org/bansheeproject/Banshee/PlayerEngine&quot;</span><span style="color: black;">&#41;</span>
pidgin = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;im.pidgin.purple.PurpleService&quot;</span>, <span style="color: #483d8b;">&quot;/im/pidgin/purple/PurpleObject&quot;</span><span style="color: black;">&#41;</span>
purple = dbus.<span style="color: black;">Interface</span><span style="color: black;">&#40;</span>pidgin, <span style="color: #483d8b;">&quot;im.pidgin.purple.PurpleInterface&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
currentTrack = banshee.<span style="color: black;">GetCurrentTrack</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
artist = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'album-artist'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
album = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'album'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
track = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'name'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
trackinfo = <span style="color: #483d8b;">&quot; %s - %s  [%s]&quot;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>track, artist, album <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> trackinfo
&nbsp;
account = purple.<span style="color: black;">PurpleAccountsFind</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'username@xmppserver.com/Home'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
buddy = purple.<span style="color: black;">PurpleFindBuddy</span><span style="color: black;">&#40;</span>account, <span style="color: #483d8b;">'update@identi.ca'</span><span style="color: black;">&#41;</span>
PURPLE_CONV_TYPE_IM = <span style="color: #ff4500;">1</span>
buddy_name = purple.<span style="color: black;">PurpleBuddyGetName</span><span style="color: black;">&#40;</span>buddy<span style="color: black;">&#41;</span>
&nbsp;
im = purple.<span style="color: black;">PurpleConvIm</span><span style="color: black;">&#40;</span>purple.<span style="color: black;">PurpleConversationNew</span><span style="color: black;">&#40;</span>PURPLE_CONV_TYPE_IM, account, buddy_name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
purple.<span style="color: black;">PurpleConvImSend</span><span style="color: black;">&#40;</span>im, <span style="color: #483d8b;">&quot;!listening to &quot;</span> + trackinfo<span style="color: black;">&#41;</span></pre></div></div>

<p>If  you want to use this, you have to replace the <em>username@xmppserver.com/Home </em>field with your xmpp account (jabber, google-talk). You also need to have the XMPP activated in identi.ca. To run, just type:</p>
<p><em>$ python filename.py </em>at the script directory.</p>
<p>This is a very simple script that gets the current track info from Banhsee and sends a message to update@identi.ca. It&#8217;s acctually very easy to add support to other music players that have a dbus interface. This is done using the python-dbus library.</p>
<p><strong>Python musictracker</strong></p>
<p>This was fun to code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env/python</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Copyright (C) 2009 Antonio Lima</span>
<span style="color: #808080; font-style: italic;"># http://www.amrlima.info</span>
<span style="color: #808080; font-style: italic;"># This program is free software; you can redistribute it and/or</span>
<span style="color: #808080; font-style: italic;"># modify it under the terms of the GNU General Public License</span>
<span style="color: #808080; font-style: italic;"># as published by the Free Software Foundation; either version 2</span>
<span style="color: #808080; font-style: italic;"># of the License, or any later version.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This program is distributed in the hope that it will be useful,</span>
<span style="color: #808080; font-style: italic;"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span style="color: #808080; font-style: italic;"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span style="color: #808080; font-style: italic;"># GNU General Public License for more details.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> dbus, gobject
<span style="color: #ff7700;font-weight:bold;">from</span> dbus.<span style="color: black;">mainloop</span>.<span style="color: black;">glib</span> <span style="color: #ff7700;font-weight:bold;">import</span> DBusGMainLoop
loop = gobject.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
DBusGMainLoop<span style="color: black;">&#40;</span>set_as_default=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
bus = dbus.<span style="color: black;">SessionBus</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
banshee = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.bansheeproject.Banshee&quot;</span>, <span style="color: #483d8b;">&quot;/org/bansheeproject/Banshee/PlayerEngine&quot;</span><span style="color: black;">&#41;</span>
purple = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;im.pidgin.purple.PurpleService&quot;</span>, <span style="color: #483d8b;">&quot;/im/pidgin/purple/PurpleObject&quot;</span><span style="color: black;">&#41;</span>
pidgin = dbus.<span style="color: black;">Interface</span><span style="color: black;">&#40;</span>purple, <span style="color: #483d8b;">&quot;im.pidgin.purple.PurpleInterface&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
STATUS_AVAILABLE = dbus.<span style="color: black;">UInt32</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
STATUS_AWAY = dbus.<span style="color: black;">UInt32</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> checkState<span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span>:
	currentTrack = banshee.<span style="color: black;">GetCurrentTrack</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;State Changed: %s&quot;</span> <span style="color: #66cc66;">%</span> state
	<span style="color: #ff7700;font-weight:bold;">if</span> state == <span style="color: #483d8b;">'playing'</span>:
		artist = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'album-artist'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		album = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'album'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		track = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'name'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		trackinfo = <span style="color: #483d8b;">&quot; %s - %s  [%s]&quot;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>track, artist, album<span style="color: black;">&#41;</span>
		old_status = purple.<span style="color: black;">PurpleSavedstatusGetCurrent</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>		<span style="color: #808080; font-style: italic;"># get current status</span>
		status_type = purple.<span style="color: black;">PurpleSavedstatusGetType</span><span style="color: black;">&#40;</span>old_status<span style="color: black;">&#41;</span>	<span style="color: #808080; font-style: italic;"># get current status type</span>
		new_status = purple.<span style="color: black;">PurpleSavedstatusNew</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span>, STATUS_AVAILABLE<span style="color: black;">&#41;</span>	<span style="color: #808080; font-style: italic;"># create new status with old status type</span>
		purple.<span style="color: black;">PurpleSavedstatusSetMessage</span><span style="color: black;">&#40;</span>new_status, trackinfo<span style="color: black;">&#41;</span>	<span style="color: #808080; font-style: italic;"># fill new status with status message</span>
		purple.<span style="color: black;">PurpleSavedstatusActivate</span><span style="color: black;">&#40;</span>new_status, trackinfo<span style="color: black;">&#41;</span>		<span style="color: #808080; font-style: italic;"># activate new status</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> state == <span style="color: #483d8b;">'paused'</span>:
		old_status = purple.<span style="color: black;">PurpleSavedstatusGetCurrent</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>		<span style="color: #808080; font-style: italic;"># get current status</span>
		status_type = purple.<span style="color: black;">PurpleSavedstatusGetType</span><span style="color: black;">&#40;</span>old_status<span style="color: black;">&#41;</span>	<span style="color: #808080; font-style: italic;"># get current status type</span>
		new_status = purple.<span style="color: black;">PurpleSavedstatusNew</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span>, STATUS_AWAY<span style="color: black;">&#41;</span>	<span style="color: #808080; font-style: italic;"># create new status with old status type</span>
		purple.<span style="color: black;">PurpleSavedstatusSetMessage</span><span style="color: black;">&#40;</span>new_status, <span style="color: #483d8b;">&quot;Banshee is paused. I'm probably not here now&quot;</span><span style="color: black;">&#41;</span>	<span style="color: #808080; font-style: italic;"># fill new status with status message</span>
		purple.<span style="color: black;">PurpleSavedstatusActivate</span><span style="color: black;">&#40;</span>new_status<span style="color: black;">&#41;</span>			<span style="color: #808080; font-style: italic;"># activate new status</span>
&nbsp;
bus.<span style="color: black;">add_signal_receiver</span><span style="color: black;">&#40;</span>checkState, dbus_interface=<span style="color: #483d8b;">&quot;org.bansheeproject.Banshee.PlayerEngine&quot;</span>, signal_name=<span style="color: #483d8b;">&quot;StateChanged&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
mainloop = gobject.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
mainloop.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is very incomplete. It has some stupid problems which I still didn&#8217;t have time or motivation to fix. One of them is really stupid for a music tracker: After running the script, you need to pause and resume Banshee in order the script picks the track info and set your status message. Or you can wait untill the next song starts <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>It&#8217;s hardcoded to set the satus to &#8220;available&#8221; while playing and &#8220;away&#8221; while paused. It picks up the chages in the music player.</p>
<p>Non ASCII characters in track info will give an error (I&#8217;m going to try to fix it) but if the next song is ASCII it will continue all right.</p>
<p>Long post! If someone (crazy people) tries these scripts and runs into trouble let me know! Maybe I can help&#8230; or not <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>EDIT: To run the scrip without having to type: $python musictracker.py every time at the command line, you can create a very simple shell script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #000000; font-weight: bold;">/</span>scriptlocation<span style="color: #000000; font-weight: bold;">/</span>musictracker.py</pre></div></div>

<p>Make it executable and if you use GNOME-DO or deskbar-applet they will index it and you can just tipe the script name and there you go, the music tracker script runs! Don&#8217;t forget to replace &#8220;scriptlocation&#8221; with the scriptl location&#8230; <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>I&#8217;m currently working (slowly) on some improvements to the script. But more on that latter.</p>
<p>Comments are welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/641/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

