Skip to content

Instantly share code, notes, and snippets.

@ebramanti
Created January 25, 2015 10:02
Show Gist options
  • Save ebramanti/c80821615c00ddcba84f to your computer and use it in GitHub Desktop.
Save ebramanti/c80821615c00ddcba84f to your computer and use it in GitHub Desktop.
Clever Average Students per Section
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type Sections struct {
Students [][]string `json:"data"`
}
type SectionsInfo struct {
Students int
Sections int
}
func (s SectionsInfo) Average() int {
return s.Students / s.Sections
}
func Unmarshal(r *http.Response, v interface{}) {
if body, err := ioutil.ReadAll(r.Body); err != nil {
log.Fatal(err)
} else if err := json.Unmarshal(body, &v); err != nil {
log.Fatal(err)
}
}
func GetSectionData() [][]string {
req, _ := http.NewRequest("GET", "https://api.clever.com/v1.1/sections?limit=1000&distinct=students", nil)
req.Header.Set("Authorization", "Bearer DEMO_TOKEN")
client := http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
var sections Sections
Unmarshal(resp, &sections)
return sections.Students
}
func main() {
sections := GetSectionData()
info := SectionsInfo{}
for _, section := range sections {
info.Students += len(section)
info.Sections += 1
}
fmt.Println(info.Average())
}

Clever Average Students per Section

For the problem, I determined the solution for visible students per section with the DEMO_TOKEN API key to be 24. I made a GET request to the /v1.1/sections route with a few modifiers (limit set to max and distinct set to return student JSON arrays in sections only).

Using Go, I wrote a struct that takes in the 2D array of data, and logic in my main function that increments an info struct that contains total students and sections. I then return the average of this result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment