|
| 1 | +import Typography from '@mui/material/Typography'; |
| 2 | +import Box from '@mui/material/Box'; |
| 3 | +import Chip from '@mui/material/Chip'; |
| 4 | +import Divider from '@mui/material/Divider'; |
| 5 | +import { StorageIcon } from '@/app/components/icons'; |
| 6 | +import CodeBlock from '@/app/components/CodeBlock'; |
| 7 | +import { getExercise } from '@/utils/exercises'; |
| 8 | + |
| 9 | +function Section({ title, children }: { title: string; children: React.ReactNode }) { |
| 10 | + return ( |
| 11 | + <Box sx={{ mb: 4 }}> |
| 12 | + <Typography variant="h6" fontWeight="bold" sx={{ mb: 1 }}> |
| 13 | + {title} |
| 14 | + </Typography> |
| 15 | + {children} |
| 16 | + </Box> |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +export default function WhyDataStructuresMatter() { |
| 21 | + const printNumber = getExercise(1, '1_print_number.py'); |
| 22 | + |
| 23 | + return ( |
| 24 | + <Box> |
| 25 | + <Chip label="Chapter 1" color="primary" size="small" sx={{ mb: 2 }} /> |
| 26 | + <Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5, mb: 1 }}> |
| 27 | + <StorageIcon color="primary" /> |
| 28 | + <Typography variant="h4" fontWeight="bold"> |
| 29 | + Why Data Structures Matter |
| 30 | + </Typography> |
| 31 | + </Box> |
| 32 | + <Typography variant="body1" color="text.secondary" sx={{ mb: 4 }}> |
| 33 | + La struttura dati che scegli determina quali operazioni puoi eseguire sui tuoi dati |
| 34 | + e quanto velocemente puoi eseguirle. |
| 35 | + </Typography> |
| 36 | + |
| 37 | + <Divider sx={{ mb: 4 }} /> |
| 38 | + |
| 39 | + <Section title="Cos'è una struttura dati?"> |
| 40 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 41 | + Una <strong>struttura dati</strong> è un modo di organizzare i dati in memoria. |
| 42 | + Le strutture dati più semplici sono gli <strong>array</strong>, ma ne esistono molte altre: |
| 43 | + set, hash table, stack, queue, linked list, alberi e grafi. |
| 44 | + </Typography> |
| 45 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 46 | + La scelta della struttura giusta può far passare un'operazione da <em>lenta</em> a <em>istantanea</em>. |
| 47 | + </Typography> |
| 48 | + </Section> |
| 49 | + |
| 50 | + <Section title="L'array: la struttura base"> |
| 51 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 52 | + Un array è una lista ordinata di elementi. Le operazioni fondamentali su un array sono: |
| 53 | + </Typography> |
| 54 | + <Box component="ul" sx={{ pl: 2 }}> |
| 55 | + {[ |
| 56 | + ['Read', 'leggere un elemento in una posizione specifica'], |
| 57 | + ['Search', 'trovare il valore di un elemento senza conoscerne la posizione'], |
| 58 | + ['Insert', 'aggiungere un elemento in una posizione specifica'], |
| 59 | + ['Delete', 'rimuovere un elemento'], |
| 60 | + ].map(([op, desc]) => ( |
| 61 | + <li key={op}> |
| 62 | + <Typography variant="body1"> |
| 63 | + <strong>{op}</strong>: {desc} |
| 64 | + </Typography> |
| 65 | + </li> |
| 66 | + ))} |
| 67 | + </Box> |
| 68 | + </Section> |
| 69 | + |
| 70 | + <Section title="Read: O(1)"> |
| 71 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 72 | + Leggere un elemento da un array è <strong>istantaneo</strong>: il computer conosce |
| 73 | + l'indirizzo in memoria del primo elemento e, dato l'indice, calcola direttamente |
| 74 | + l'indirizzo dell'elemento cercato. |
| 75 | + </Typography> |
| 76 | + <CodeBlock>{'array[3] // 1 solo step, sempre'}</CodeBlock> |
| 77 | + </Section> |
| 78 | + |
| 79 | + <Section title="Search: O(N)"> |
| 80 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 81 | + Cercare un valore senza conoscerne l'indice richiede di scorrere l'array elemento |
| 82 | + per elemento. Nel caso peggiore (valore non presente o in ultima posizione), eseguiamo |
| 83 | + tanti step quanti sono gli elementi: <strong>N step</strong>. |
| 84 | + </Typography> |
| 85 | + <CodeBlock>{`// Linear search — worst case: N steps |
| 86 | +for (let i = 0; i < array.length; i++) { |
| 87 | + if (array[i] === target) return i; |
| 88 | +}`}</CodeBlock> |
| 89 | + </Section> |
| 90 | + |
| 91 | + <Section title="Insert e Delete"> |
| 92 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 93 | + Inserire o eliminare un elemento <em>in mezzo</em> a un array è costoso: tutti gli |
| 94 | + elementi successivi devono essere shiftati. L'inserimento in fondo è invece O(1). |
| 95 | + </Typography> |
| 96 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 97 | + Questo è il motivo per cui in certi scenari conviene usare strutture diverse, |
| 98 | + come le <strong>linked list</strong>, che vedremo nei capitoli successivi. |
| 99 | + </Typography> |
| 100 | + </Section> |
| 101 | + |
| 102 | + <Section title="Set: una variante degli array"> |
| 103 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 104 | + Un <strong>set</strong> è come un array, ma non permette valori duplicati. |
| 105 | + Il costo di questa garanzia è che ogni inserimento richiede prima una ricerca |
| 106 | + (per verificare che il valore non esista già), portando la complessità a <strong>O(N)</strong>. |
| 107 | + </Typography> |
| 108 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 109 | + Quando i duplicati non hanno senso nel tuo dominio, i set sono la scelta giusta. |
| 110 | + </Typography> |
| 111 | + </Section> |
| 112 | + |
| 113 | + <Section title={`Esercizio — ${printNumber.filename}`}> |
| 114 | + <Typography variant="body1" sx={{ mb: 2 }}> |
| 115 | + Due implementazioni per stampare i numeri pari fino a 100. La seconda dimezza |
| 116 | + il numero di step saltando direttamente di 2 in 2. |
| 117 | + </Typography> |
| 118 | + <CodeBlock language={printNumber.language}>{printNumber.code}</CodeBlock> |
| 119 | + </Section> |
| 120 | + </Box> |
| 121 | + ); |
| 122 | +} |
0 commit comments