(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* :Name: EPM`Introduction` *)


(* :Book: Essentials of Programming in Mathematica *)


(* :Author: Paul Wellin *)


(* :Summary: This package contains the functions defined in Chapter 1 *)


(* :Context: EPM`Introduction` *)


(* :Package Version: 1.1 *)


(* :Copyright: Copyright 2015, Paul R Wellin *)


(* :History: Version 1.0 by Paul Wellin, Dec 2012
			Version 1.1 by Paul Wellin July 2014 *)


(* :Keywords: leading digit, pass phrase, StringMemberQ *)


(* :Source: Introduction.nb *)


(* :Mathematica Version: 10.3 *)


(* Tested in Mathematica 11.1 *)


Remove[LeadingDigit,LeadingDigitChart,PassPhrase,StringMemberQ]


(* BeginPackage["EPM`Introduction`"] *)


LeadingDigit::usage="LeadingDigit[\!\(\*
StyleBox[\"n\", \"TI\"]\)] returns the leading digit of the integer \!\(\*
StyleBox[\"n\", \"TI\"]\).";


LeadingDigitChart::usage="LeadingDigitChart[\!\(\*
StyleBox[\"data\", \"TI\"]\)] makes a horizontal bar chart showing the distribution of the leading digits among the \!\(\*
StyleBox[\"data\", \"TI\"]\).";


LeadingDigitChart::badinput="The argument to LeadingDigitChart must be an array of numerical data.";


PassPhrase::usage="PassPhrase[\!\(\*
StyleBox[\"n\", \"TI\"]\)\!\(\*
StyleBox[\",\", \"TI\"]\)\!\(\*
StyleBox[\" \", \"TI\"]\)\!\(\*
StyleBox[\"{\", \"TI\"]\)\!\(\*
StyleBox[\"min\", \"TI\"]\)\!\(\*
StyleBox[\",\", \"TI\"]\)\!\(\*
StyleBox[\" \", \"TI\"]\)\!\(\*
StyleBox[\"max\", \"TI\"]\)\!\(\*
StyleBox[\"}\", \"TI\"]\)] generates a phrase of \!\(\*
StyleBox[\"n\", \"TI\"]\) random words from the dictionary each of string length from \!\(\*
StyleBox[\"min\", \"TI\"]\) to \!\(\*
StyleBox[\"max\", \"TI\"]\).";


StringMemberQ::usage="StringMemberQ[\!\(\*
StyleBox[\"str\", \"TI\"]\)\!\(\*
StyleBox[\",\", \"TI\"]\)\!\(\*
StyleBox[\" \", \"TI\"]\)\!\(\*
StyleBox[\"chars\", \"TI\"]\)] returns True if the list of string characters, \!\(\*
StyleBox[\"chars\", \"TI\"]\) are all present in the string \!\(\*
StyleBox[\"str\", \"TI\"]\).";


Options[BondPercolation]=Options[Graph];


Options[LeadingDigitChart]=Options[BarChart];


Begin["`Private`"]


LeadingDigit[n_Integer]:=First[IntegerDigits[n]]


ToPercents[lis_]:=Map[StringJoin[ToString[SetPrecision[#,3]],"%"]&,100N[lis/Total[lis]]]


LeadingDigitChart[data_,opts:OptionsPattern[]]:=Module[{digCounts,ratios,labels},
If[Not@ArrayQ[data,1,NumericQ],Message[LeadingDigitChart::badinput],
digCounts=Sort@Tally[data][[All,2]];
labels=ToPercents[digCounts];BarChart[MapThread[Labeled[#1,#2,Right]&,{digCounts,labels}],
opts,BarOrigin->Left,
ChartLabels->Reverse@Range[Length[labels]],
ChartStyle->47]]
]


PassPhrase[n_,{min_:2,max_:6}]:=Module[{words},
words=DictionaryLookup[w__/;min<=StringLength[w]<=max];
StringJoin[Riffle[RandomChoice[words,n]," "]]
]


StringMemberQ[str_String,chars_List]:=SubsetQ[Characters[str],chars]


End[]


(*EndPackage[]*)



