← Catalogo
INI5Python Village
PyJS

Working with Files

Python Village - non fa parte del sistema di Topics ufficiale di Rosalind (riservato a Bioinformatics Stronghold/Armory).

Pagina originale su rosalind.info

Descrizione

Esercizio introduttivo su lettura e scrittura di file in Python (modalità 'r'/'w'/'a', metodi read(), readline(), readlines(), split(), write()).

Given

Un file contenente al massimo 1000 righe.

Return

Un file contenente tutte le righe con numero pari dal file originale (indicizzazione delle righe 1-based).

Sample Dataset

Bravely bold Sir Robin rode forth from Camelot
Yes, brave Sir Robin turned about
He was not afraid to die, O brave Sir Robin
And gallantly he chickened out
He was not at all afraid to be killed in nasty ways
Bravely talking to his feet
Brave, brave, brave, brave Sir Robin
He beat a very brave retreat

Sample Output

Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat

La mia esecuzione

08/24/2026 11:24:47

Input · dataset.txt

`Twas brillig, and the slithy toves
Some things in life are bad, they can really make you mad
  Did gyre and gimble in the wabe:
Other things just make you swear and curse
All mimsy were the borogoves,
When you're chewing on life's gristle, don't grumble give a whistle
  And the mome raths outgrabe.
This will help things turn out for the best
"Beware the Jabberwock, my son!
Always look on the bright side of life
  The jaws that bite, the claws that catch!
Always look on the right side of life
Beware the Jubjub bird, and shun
If life seems jolly rotten, there's something you've forgotten
  The frumious Bandersnatch!"
And that's to laugh and smile and dance and sing
He took his vorpal sword in hand:
When you're feeling in the dumps, don't be silly, chumps
  Long time the manxome foe he sought --
Just purse your lips and whistle, that's the thing
So rested he by the Tumtum tree,
So, always look on the bright side of death
  And stood awhile in thought.
Just before you draw your terminal breath
And, as in uffish thought he stood,
Life's a counterfeit and when you look at it
  The Jabberwock, with eyes of flame,
Life's a laugh and death's the joke, it's true
Came whiffling through the tulgey wood,
You see, it's all a show, keep them laughing as you go
  And burbled as it came!
Just remember the last laugh is on you
One, two! One, two! And through and through
Always look on the bright side of life
  The vorpal blade went snicker-snack!
And always look on the right side of life
He left it dead, and with its head
Always look on the bright side of life
  He went galumphing back.
And always look on the right side of life

Output · run_log.txt

OK
Some things in life are bad, they can really make you mad
Other things just make you swear and curse
When you're chewing on life's gristle, don't grumble give a whistle
This will help things turn out for the best
Always look on the bright side of life
Always look on the right side of life
If life seems jolly rotten, there's something you've forgotten
And that's to laugh and smile and dance and sing
When you're feeling in the dumps, don't be silly, chumps
Just purse your lips and whistle, that's the thing
So, always look on the bright side of death
Just before you draw your terminal breath
Life's a counterfeit and when you look at it
Life's a laugh and death's the joke, it's true
You see, it's all a show, keep them laughing as you go
Just remember the last laugh is on you
Always look on the bright side of life
And always look on the right side of life
Always look on the bright side of life
And always look on the right side of life

Esegui nel browser · Pyodide

Mostra il codice sorgente (problem.py)
filename = "dataset.txt"
fileOut = "fileOut.txt"
with open(filename) as f:
    testo = list(f)
with open(fileOut, "w") as f:
    for i in range(len(testo)):
        if i%2==1:
           print(testo[i].rstrip("\n"))
           f.write(testo[i])	

Soluzione JavaScript

Esegui ora, live

Mostra il codice sorgente
// ini5 - esercizio introduttivo "Python Village": stampa le righe di
// indice dispari (0-indexed) del file di input. Soluzione JavaScript
// indipendente, non una trascrizione di problem.py, riscritta in modo
// idiomatico per JS.
//
// Nota: problem.py scrive anche le stesse righe su un file locale
// fileOut.txt, ma quell'effetto collaterale non fa parte dell'output
// atteso da Rosalind (che valuta solo lo stdout) - qui viene omesso
// perché il contratto di questa funzione è "testo in, testo out".
//
// Contratto: riceve il contenuto testuale di dataset.txt, restituisce
// l'output testuale (stessa forma dell'output Python, cioè dello stdout).
export default function solve(datasetText) {
  const righe = datasetText.split("\n");

  const output = righe
    .filter((_, i) => i % 2 === 1)
    .map((riga) => riga.replace(/\r?\n?$/, ""));

  return output.join("\n") + "\n";
}