samedi 9 janvier 2016

How to draw a Huffman tree properly

Vote count: 0

I have the following symbols and probabilities and I would like to draw a Huffman tree for them:

s = 0.04 || i = 0.1 || n = 0.2 || b = 0.04 || a = 0.3 || d = 0.26 || ~ = 0.06

Based on Huffman algorithm, I generated the following tree:

enter image description here

This was done by:

  1. Join s + i
  2. Join the result of 1 and n
  3. Join ~ + d
  4. Join b + a
  5. Join the result of 3 and 4
  6. Join the result of 5 and 2

My questions: is what I have done right or not? If so, is it acceptable that the final probability (result of 6) greater than 1?

Thanks

asked 1 min ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



How to draw a Huffman tree properly

php, sql INSERT TO not working when ACTION links to another page

Vote count: 0

My player message system isn't working. You start on the Diplomacy page which lists the player nations. Once a player nation is selected they are sent to the send message page which shows any messages between themselves and the nation selected. It also gives them a message box to write their own message to send. Here is the code.

if(isset($_POST['message']) && !empty($_POST['message'])){
    $sender = $_GET['nation'];
    $receiver = $_GET['receiver'];
    $random_number = rand();
    $message = $_POST['message'];
    $type = $_GET['type'];

    $check_con = mysql_query("SELECT `hash` FROM `message_group` WHERE (`user_one`='$sender' AND `user_two`='$receiver') OR (`user_one`='$receiver' AND `user_two`='$sender')");

    if(mysql_num_rows($check_con) ===1){
        $get_hash = mysql_fetch_assoc($check_con);

        $hash = $get_hash['hash'];
        mysql_query("INSERT INTO messages (group_hash, from_id, message, seen) VALUES('$hash','$sender','$message','0')");
        echo "<p>Message Sent!</p>";
   }else{   
        mysql_query("INSERT INTO message_group (user_one, user_two, hash) VALUES('$sender','$receiver','$random_number')");
        mysql_query("INSERT INTO messages (group_hash, from_id, message, seen) VALUES('$random_number','$sender','$message','0')");
        echo "<p>Conversation Started!</p>"; 
    }
    }
    ?>
    <form method="POST" action="index.php?page=gc3025/send_beta.php&game=<?php echo $game; ?>&type=<?php echo $type; ?>&nation=<?php echo $nations_id; ?>&user=<?php echo $user_id; ?>&receiver=<?php echo $receiver_id; ?>">
    <table>
    Enter Message:
    <tr>
   <td></td>
   <td><textarea name='message' rows='7' cols='60'></textarea></td>
   </tr>
    <td><input type='submit' value="Send Message" /></td>
    </table>
    </form>

If under FORM ACTION I link the page to itself it works but you have to refresh the page to see the new message which also resends the message. If the FORM ACTION goes to the previous page then it does not INSERT the message into the table on the server.

asked 1 min ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



php, sql INSERT TO not working when ACTION links to another page

Python Using A Microphone

Vote count: 0

I was watching Avengers and I thought it would be it wresting to make something like Jarvis with Python which is what I'm doing. I've only been doing Python for 1 year ( I'm 15) so I'm not the best.

So basically, I want to make something basic at the beginning where if I say "Cameron, activate" it will out put to the speakers "Cameron activated". Them if I say "hello" it'll reply "hello".

I first want to know if this is possible and if so, could someone help me with it?? Thanks so much!

asked 29 secs ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



Python Using A Microphone

jQuery validation error "not defined"

Vote count: 0

When validating the jQuery I receive the followers 2 errors: "'$' was used before it was defined." & "'window' was used before it was defined." What does this mean? & how do I remove these errors? Thank you :-)

var lastScrollTop = 0;
$(window).scrollTop(0);

$(window).on('scroll', function() {
    var header = $('header');
    var content = $('content');
    var headerBg = $('.header-bg');
    var headerCnt = $('.header-content');
    var scrollTop = $(window).scrollTop();
    var dynHeaderVisible = false;
  
    if (lastScrollTop > scrollTop) {
      if (scrollTop <= 400) {
        headerBg.css("height", 0);
        headerCnt.css('color', 'white');
      } else {
        headerBg.css("height", 80);
        headerCnt.css("height", 80);
        headerCnt.css('color', 'black');
      }
    } else {
      // Down
      if (scrollTop > 350) {
        console.log ("hi");
        headerCnt.css("height", 0);
        headerBg.css("height", 0);
      }
    }
    
    lastScrollTop = scrollTop;
});

$.fn.isOnScreen = function(){
    var element = this.get(0);
    var bounds = element.getBoundingClientRect();
    return bounds.top < window.innerHeight && bounds.bottom > 0;
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  width: 100%;
  margin: 0;
  list-style: none;
  text-decoration: none;
  font-size:1em;
  font-family: Helvetica Neue, Helvetica, Arial, Sans-serif;
  }
        a {
    background:transparent;
    border:none;
    letter-spacing:0.15em;
    text-transform:uppercase;
    transition: .3s color;
        transition: .3s height;
        }


header {
  display: block;
  position: fixed;
  height: 80px;
  width: 100%;
}

header ul {
  z-index: 20;
}

.header-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: transparent;
}

.header-bg,
.header-content {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  text-align: center;
}

.header-bg {
  z-index: 100;
  color: gray;
  background-color: white;
  border-bottom: 1px solid black;
  transition: .3s height;
  height: 0;
}

.header-content {
  z-index: 200;
  transition: .3s color;
  color: white;
  background-color: transparent;
  height: 80px;
  transition: .3s height;
  overflow: hidden;
  list-style: none;
}

.logo {
    position: absolute;
    left: 47%;
        color: inherit;
    display: inline-block;
    width: 15px;
    height: 15px;
    padding: 18px;
    cursor: pointer;
    font-size:.8em;
    letter-spacing:0.05em;
        transition: .3s color;
        }

content {
  display: block;
  height: 2000px;
  background-color: orange;
}

.stage {
    color: #fff;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    height: 100vh;
    background-color: white;
        border-bottom: 1px solid black;
    font-size: 48px;
        height: 200px;
        width: 100%;
}

.stage-0 {
    background: grey;
    height: 400px;
}
<script src=     "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<header>
  <div class="header-wrapper">
    <div class="header-bg"></div>
    <div class="header-content">
      <ul>
        <li>
        <a href="" class="logo">Logo </a>
        </li>
      </ul>
    </div>
  </div>
</header>
<content>
<div class="stage stage-0">1</div>
<div class="stage stage-2">3</div>
<div class="stage stage-4">5</div>
<div class="stage stage-6">7</div>
<div class="stage stage-8">9</div>
<div class="stage stage-10">11</div>
<div class="stage stage-12">13</div>
<div class="stage stage-14">15</div>
<div class="stage stage-16">17</div>
<div class="stage stage-18">19</div>
<div class="stage stage-20">21</div>
<div class="stage stage-22">23</div>
</content>
asked 30 secs ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



jQuery validation error "not defined"

Choropleth example in D3 is not working properly

Vote count: 0

I have this straight forward example where I wanted to use my data and my topojson instead. I get the data loaded but the visualization is not working properly. Data is loaded properly

Logging the dataRange() gives my -INFINITY,INFINITY, so something with the ranging is noch wokring but I can't see what it is.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>d3 choropleth map</title>
    <style>
      #wrapper {
          width: 960px;
          margin: -30px auto 0;
      }
      #map {
          width: 960px;
          height: 580px;
          position: relative;
      }
      .stroke {
        fill: none;
        stroke: #888;
        stroke-width: 2px;
      }

      .fill {
        fill: #fff;
      }

      .graticule {
        fill: none;
        stroke: #777;
        stroke-width: .5px;
        stroke-opacity: .5;
      }

      .land {
        fill: #222;
      }

      .boundary {
        fill: none;
        stroke: #fff;
        stroke-width: .5px;
      }
      .country {
          fill: steelblue;
          stroke: white;
      }
      #play, #clock {
        position: absolute;
        top: 15px;
      }
      #play {
        left: 15px;
      }
      #clock {
        left: 65px;
      }
    </style>
</head>

<body>

    <div id="wrapper">
        <div id="map"></div>
      <button id="play">play</button>
      <span id="clock">year</span>
    </div>


<script src="http://ift.tt/JmLP3T"></script>
<script src="http://ift.tt/1oOECWY"></script>
<script src="http://ift.tt/1hUyqi2"></script>
<script src="http://ift.tt/JmLP3U"></script>
<script>
  //globals
var width, height, projection, path, graticule, svg, attributeArray = [], currentAttribute = 0, playing = false;




function init() {

  setMap();
  animateMap();

}

function setMap() {

  width = 960, height = 580;  // map width and height, matches

  var projection = d3.geo.mercator()
      .translate([width / 2, height])
      //.scale((width - 1) / 2 / Math.PI);
      .scale(400);

  path = d3.geo.path()  // create path generator function
      .projection(projection);  // add our define projection to it

  graticule = d3.geo.graticule(); // create a graticule

  svg = d3.select("#map").append("svg")   // append a svg to our html div to hold our map
      .attr("width", width)
      .attr("height", height);

  svg.append("defs").append("path")   // prepare some svg for outer container of svg elements
      .datum({type: "Sphere"})
      .attr("id", "sphere")
      .attr("d", path);

  svg.append("use")   // use that svg to style with css
      .attr("class", "stroke")
      .attr("xlink:href", "#sphere");

  svg.append("path")    // use path generator to draw a graticule
      .datum(graticule)
      .attr("class", "graticule")
      .attr("d", path);

  loadData();  // let's load our data next

}

function loadData() {

  queue()   // queue function loads all external data files asynchronously
    .defer(d3.json, "mapdata/germany.json")  // our geometries
    .defer(d3.csv, "choroplethmap.csv")  // and associated data in csv file
    .await(processData);   // once all files are loaded, call the processData function passing
                           // the loaded objects as arguments
}

function processData(error,world,countryData) {
  // function accepts any errors from the queue function as first argument, then
  // each data object in the order of chained defer() methods above

  var countries = world.objects.states_germany.geometries  // store the path in variable for ease
  //console.log(countries);
  for (var i in countries) {    // for each geometry object
    for (var j in countryData) {  // for each row in the CSV
      console.log(countryData[j]);
      if(countries[i].id == countryData[j].id) {
        //console.log("SUCCESS");   // if they match
        for(var k in countryData[i]) {   // for each column in the a row within the CSV
          if(k != 'name' && k != 'id') {  // let's not add the name or id as props since we already have them
            if(attributeArray.indexOf(k) == -1) {
               attributeArray.push(k);  // add new column headings to our array for later
            }
            countries[i][k] = Number(countryData[j][k])  // add each CSV column key/value to geometry object
          }
        }
        break;  // stop looking through the CSV since we made our match
      }
    }
  }
  d3.select('#clock').html(attributeArray[currentAttribute]);  // populate the clock initially with the current year
  drawMap(world);  // let's mug the map now with our newly populated data object
}

function drawMap(world) {

    svg.selectAll(".country")   // select country objects (which don't exist yet)
      .data(topojson.feature(world, world.objects.states_germany).features)  // bind data to these non-existent objects
      .enter().append("path") // prepare data to be appended to paths
      .attr("class", "country") // give them a class for styling and access later
      .attr("id", function(d) { return "code_" + d.properties.id; }, true)  // give each a unique id for access later
      .attr("d", path); // create them using the svg path generator defined above

    var dataRange = getDataRange(); // get the min/max values from the current year's range of data values
    d3.selectAll('.country')  // select all the countries
    .attr('fill-opacity', function(d) {
        return getColor(d[attributeArray[currentAttribute]], dataRange);  // give them an opacity value based on their current value
    });
}

function sequenceMap() {

    var dataRange = getDataRange(); // get the min/max values from the current year's range of data values
    d3.selectAll('.country').transition()  //select all the countries and prepare for a transition to new values
      .duration(750)  // give it a smooth time period for the transition
      .attr('fill-opacity', function(d) {
        return getColor(d[attributeArray[currentAttribute]], dataRange);  // the end color value
      })

}

function getColor(valueIn, valuesIn) {

  var color = d3.scale.linear() // create a linear scale
    .domain([valuesIn[0],valuesIn[1]])  // input uses min and max values
    .range([.3,1]);   // output for opacity between .3 and 1 %

  return color(valueIn);  // return that number to the caller
}

function getDataRange() {
  // function loops through all the data values from the current data attribute
  // and returns the min and max values

  var min = Infinity, max = -Infinity;
  d3.selectAll('.country')
    .each(function(d,i) {
      var currentValue = d.properties[attributeArray[currentAttribute]];
      if(currentValue <= min && currentValue != -99 && currentValue != 'undefined') {
        min = currentValue;
      }
      if(currentValue >= max && currentValue != -99 && currentValue != 'undefined') {
        max = currentValue;
      }
  });
  console.log([min,max]);
  return [min,max];  //boomsauce
}

function animateMap() {

  var timer;  // create timer object
  d3.select('#play')
    .on('click', function() {  // when user clicks the play button
      if(playing == false) {  // if the map is currently playing
        timer = setInterval(function(){   // set a JS interval
          if(currentAttribute < attributeArray.length-1) {
              currentAttribute +=1;  // increment the current attribute counter
          } else {
              currentAttribute = 0;  // or reset it to zero
          }
          sequenceMap();  // update the representation of the map
          d3.select('#clock').html(attributeArray[currentAttribute]);  // update the clock
        }, 2000);

        d3.select(this).html('stop');  // change the button label to stop
        playing = true;   // change the status of the animation
      } else {    // else if is currently playing
        clearInterval(timer);   // stop the animation by clearing the interval
        d3.select(this).html('play');   // change the button label to play
        playing = false;   // change the status again
      }
  });
}


window.onload = init();  // magic starts here



</script>
</body>
</html>

So where is the mistake? You can finde the full data (map etc) here

asked 44 secs ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



Choropleth example in D3 is not working properly

Convert javascript class instance to plain object preserving methods

Vote count: 0

I want to convert an instance class to plain object, without losing methods and/or inherited properties. So for example:

class Human {
    height: number;
    weight: number;
    constructor() {
        this.height = 180;
        this.weight = 180;
    }
    getWeight() { return this.weight; }
    // I want this function to convert the child instance
    // accordingly
    toJSON() {
        // ???
        return {};
    }
}
class Person extends Human {
    public name: string;
    constructor() {
        super();
        this.name = 'Doe';
    }
    public getName() {
        return this.name;
    }
}
class PersonWorker extends Person {
    constructor() {
        super();
    }
    public report() {
        console.log('I am Working');
    }
    public test() {
        console.log('something');
    }
}
let p = new PersonWorker;
let jsoned = p.toJSON();

jsoned should look like this:

{
    // from Human class
    height: 180,
    weight: 180,
    // when called should return this object's value of weight property
    getWeight: function() {return this.weight},

    // from Person class
    name: 'Doe'
    getName(): function() {return this.name},

    // and from PersonWorker class
    report: function() { console.log('I am Working'); },

    test: function() { console.log('something'); }
}

Is this possible to achieve, and if so, how?

In case you're wondering, I need this because I am using a framework that, unfortunately, accepts as input only a json object, whereas I am trying to use typescript and class inheritance.

asked 57 secs ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



Convert javascript class instance to plain object preserving methods

Android Studio cannot resolve symbol ITelephony

Vote count: 0

I'm working on an Android app that is supposed to block incoming calls and (eventually) messages. So far I've read that you can use the internal ITelephony interface to intercept and eventually block calls.

One step of that process involves creating a package called "com.android.internal.telephony" containing a "ITelephony.aidl" file. I've got the following path right now:

\MyApp\app\src\main\java\company\MyApp\com\android\internal\telephony\ITelephony.aidl (this is what Android Studio creates with the "Create Package" command.

The issue is that, for some reason, I get the following error:

error: package com.android.internal.telephony does not exist. I've tried placing the aidl file in different locations but with the same result. Am I doing something wrong and, if so, how do I solve this?

Thanks.

asked 1 min ago

This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at http://ift.tt/jcXqJW.



Android Studio cannot resolve symbol ITelephony