core_templates2/docdiff.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 01 Nov 2022 15:03:48 +0000
changeset 428 cdfa6a293453
parent 396 3ffe978a5664
child 482 769bda18a43d
permissions -rw-r--r--
updated solutions and templates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
     1
// Core Part 2 about Code Similarity
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
     2
//===================================
283
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
     3
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
     4
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
     5
object C2 { 
283
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
     6
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
     7
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
     8
//======================
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    10
//(1)
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 283
diff changeset
    11
def clean(s: String) : List[String] = ???
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    15
//(2)
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 283
diff changeset
    16
def occurrences(xs: List[String]): Map[String, Int] = ???
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    19
//(3)
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 283
diff changeset
    20
def prod(lst1: List[String], lst2: List[String]) : Int = ???
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    23
//(4)
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 283
diff changeset
    24
def overlap(lst1: List[String], lst2: List[String]) : Double = ???
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 283
diff changeset
    26
def similarity(s1: String, s2: String) : Double = ???
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
/* Test cases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
val list1 = List("a", "b", "b", "c", "d") 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val list2 = List("d", "b", "d", "b", "d")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
occurrences(List("a", "b", "b", "c", "d"))   // Map(a -> 1, b -> 2, c -> 1, d -> 1)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
occurrences(List("d", "b", "d", "b", "d"))   // Map(d -> 3, b -> 2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
prod(list1,list2) // 7 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
overlap(list1, list2)   // 0.5384615384615384
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
overlap(list2, list1)   // 0.5384615384615384
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
overlap(list1, list1)   // 1.0
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
overlap(list2, list2)   // 1.0
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// Plagiarism examples from 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// https://desales.libguides.com/avoidingplagiarism/examples
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
val orig1 = """There is a strong market demand for eco-tourism in
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
Australia. Its rich and diverse natural heritage ensures Australia's
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
capacity to attract international ecotourists and gives Australia a
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
comparative advantage in the highly competitive tourism industry."""
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
val plag1 = """There is a high market demand for eco-tourism in
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
Australia. Australia has a comparative advantage in the highly
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
competitive tourism industry due to its rich and varied natural
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
heritage which ensures Australia's capacity to attract international
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
ecotourists."""
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
similarity(orig1, plag1) // 0.8679245283018868
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
// Plagiarism examples from 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
// https://www.utc.edu/library/help/tutorials/plagiarism/examples-of-plagiarism.php
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
val orig2 = """No oil spill is entirely benign. Depending on timing and
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
location, even a relatively minor spill can cause significant harm to
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
individual organisms and entire populations. Oil spills can cause
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
impacts over a range of time scales, from days to years, or even
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
decades for certain spills. Impacts are typically divided into acute
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
(short-term) and chronic (long-term) effects. Both types are part of a
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
complicated and often controversial equation that is addressed after
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
an oil spill: ecosystem recovery."""
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
val plag2 = """There is no such thing as a "good" oil spill. If the
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
time and place are just right, even a small oil spill can cause damage
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
to sensitive ecosystems. Further, spills can cause harm days, months,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
years, or even decades after they occur. Because of this, spills are
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
usually broken into short-term (acute) and long-term (chronic)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
effects. Both of these types of harm must be addressed in ecosystem
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
recovery: a controversial tactic that is often implemented immediately
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
following an oil spill."""
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
overlap(clean(orig2), clean(plag2))  // 0.728
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
similarity(orig2, plag2)             // 0.728
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
// The punchline: everything above 0.6 looks suspicious and 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// should be investigated by staff.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
*/
283
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
    93
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
    94
}