


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
WGU D793 Formal Languages Task 1 and 2 Guide: Programming Paradigms Analysis and Code Conversion from FORTRAN to Python – 2026 Updated with complete solutions.
Typology: Exams
1 / 34
This page cannot be seen from the preview
Don't miss anything!



























WGU D793 Formal Languages Study Guide Task 1: Programming Paradigms Analysis Understanding the Core Paradigms For Task 1, you need to analyze how different programming paradigms approach problem- solving. Here are the four paradigms you'll encounter: Imperative Paradigm focuses on explicit step-by-step instructions that change program state. Think of it as giving someone a detailed recipe - "do this, then do that, then do this." FORTRAN exemplifies this paradigm, using DO loops, GOTO statements, and mutable variables to manipulate data directly. The programmer controls exactly how and when operations execute. Functional Paradigm treats computation as mathematical function evaluation without mutable state or side effects. Python supports functional features like map(), filter(), and lambda expressions, though it's not purely functional. The key difference from imperative is that you describe what to compute rather than how to compute it. Object-Oriented Paradigm organizes code around objects that contain both data and methods. Python excels here with classes, inheritance, and polymorphism. This contrasts with FORTRAN's procedural approach where data and operations remain separate.
WGU D793 Formal Languages - Complete 60-Page Case Study Guide Task 1 & Task 2: Programming Paradigms Analysis and FORTRAN to Python Conversion PART ONE: EXECUTIVE SUMMARY (Pages 1-3) Course Overview WGU D793 Formal Languages requires students to master two primary competencies:
Why This Matters Understanding formal languages and programming paradigms is foundational to computer science. FORTRAN, despite being developed in the 1950s, remains critical in scientific computing. Converting legacy FORTRAN to Python preserves decades of validated scientific code while leveraging modern language features. PART TWO: TASK 1 - PROGRAMMING PARADIGMS ANALYSIS (Pages 4-25) Chapter 1: Understanding Programming Paradigms (Pages 4-8) 1.1 Definition and Significance A programming paradigm is a fundamental style of computer programming that determines how developers conceptualize and structure solutions. Paradigms define:
FORTRAN Support Native Limited None None Python Support Native Strong Strong Moderate Chapter 2: Deep Dive - The Imperative Paradigm (Pages 9-14) 2.1 FORTRAN as the Quintessential Imperative Language FORTRAN (FORmula TRANslation) was designed in 1957 specifically for scientific computing. Its imperative nature manifests through: Sequential Execution fortran PROGRAM sequential_example IMPLICIT NONE INTEGER :: x, y, z x = 5_! Statement 1_ y = 10_! Statement 2_ z = x + y_! Statement 3_ PRINT *, z_! Statement 4_ END PROGRAM State Modification fortran SUBROUTINE modify_state(arr, n) IMPLICIT NONE INTEGER, INTENT(INOUT) :: arr(n) INTEGER :: i DO i = 1 , n arr(i) = arr(i) * 2_! Direct state modification_
2.2 Key FORTRAN Imperative Features DO Loops : The primary iteration construct fortran DO i = 1 , 100 , 2 IF (i > 50 ) EXIT array(i) = i ** 2 END DO GOTO Statements : Unconditional branching (now discouraged but historically important) fortran IF (error_flag .NE. 0 ) GOTO 999 ! Normal execution continues 999 CONTINUE Array Operations : Column-major storage and direct manipulation fortran DIMENSION matrix( 10 , 10 ) DO i = 1 , 10 DO j = 1 , 10 matrix(i, j) = i * j END DO END DO Chapter 3: Deep Dive - The Functional Paradigm (Pages 15-18) 3.1 Core Principles
reduce() reduce(func, iterable) Accumulate values lambda lambda x: x2 Anonymous functions list comprehensions [x2 for x in list] Declarative list creation Chapter 4: Deep Dive - Object-Oriented Paradigm (Pages 19-22) 4.1 FORTRAN's OOP Limitations Traditional FORTRAN (pre-2003) has no native OOP support. This is a critical distinction for Task 1 analysis. FORTRAN 77 and 90:
def precision(self): return self._precision @precision.setter def precision(self, value): if value > 0 : self._precision = value def calculate(self, operation, args): """Polymorphic method - works with any operation""" result = operation(args) self._history.append(result) return result def get_history(self): return self._history.copy() # Encapsulated access class EnhancedCalculator(ScientificCalculator): # Inheritance def init(self, precision= 6 , mode='standard'): super().init(precision) self.mode = mode def calculate_with_logging(self, operation, *args): result = super().calculate(operation, *args) print(f"Calculation result: {result}") return result 4.3 Paradigm Transition Analysis Key Insight for Task 1 : When converting FORTRAN to Python, you're not just translating syntax - you're transitioning from imperative-only to multi-paradigm programming. This enables:
Criterion FORTRAN (Imperative) Python Implementation State Management [Describe] [Describe] Control Flow [Describe] [Describe] Data Abstraction [Describe] [Describe] Code Organization [Describe] [Describe] Paradigm Shift Observations: [Explain how the Python version could leverage OOP or functional features] text
Based on current industry practices, there are four primary approaches to converting FORTRAN to Python [citation:1]:
StrategyBest ForComplexitySpeed Manual TranslationSmall programs (<500 lines)HighSlow f2py WrapperLarge numerical codesMediumFast execution Hybrid ApproachMixed legacy systemsHighMedium Full RewriteBusiness-critical appsVery HighVery SlowStep-by-Step Process:
# STEP 1: Understand the FORTRAN code structure # FORTRAN code: PROGRAM main IMPLICIT NONE INTEGER :: i, n REAL :: x(100), result n = 100 DO i = 1, n x(i) = i * 2. END DO result = calculate_sum(x, n) PRINT *, 'Sum = ', result END PROGRAM FUNCTION calculate_sum(arr, n) IMPLICIT NONE INTEGER :: n, i REAL :: arr(n), calculate_sum, temp temp = 0. DO i = 1, n temp = temp + arr(i) END DO calculate_sum = temp END FUNCTION python _# STEP 2: Map FORTRAN types to Python # FORTRAN Type - > Python Type # INTEGER - > int # REAL - > float # CHARACTER - > str # LOGICAL - > bool # DIMENSION - > list or numpy.array # STEP 3: Write equivalent Python_ import numpy as np DO i=1,10,2 for i in range(1, 11, 2): Step parameter IF (x .GT. y) THEN if x > y: Use Python operators ELSE IF elif Combined keyword GOTO label **Avoid** - use functions/loops No direct equivalent SUBROUTINE name() def name(): Functions replace subroutines FUNCTION name() def name(): return Functions return values IMPLICIT NONE Not needed Python is dynamically typed INTEGER :: a(10) a = [0]*10 or np.zeros(10) List or numpy array READ(*,*) var var = input() Console input WRITE(*,*) var print(var) Console output OPEN(unit, file) with open(file) as f: Context manager .AND., .OR., .NOT. and, or, not Logical operators .EQ., .NE., .GT. ==, !=, > Comparison operators **Chapter 7: Complete Code Conversion Examples (Pages 36-50) 7.1 Example 1: Numerical Integration (Trapezoidal Rule) FORTRAN Source:** fortran PROGRAM trapezoidal ## IMPLICIT NONE REAL :: a, b, h, sum, x INTEGER :: n, i REAL, EXTERNAL :: f a = 0. b = 1. n = 1000 h = (b - a) / n sum = (f(a) + f(b)) / 2. DO i = 1 , n- 1 x = a + i * h sum = sum + f(x) END DO sum = sum * h PRINT *, 'Integral = ', sum END PROGRAM FUNCTION f(x) REAL :: f, x f = x** 2 + 2 *x + 1 END FUNCTION **Python Conversion (Imperative):** python def f(x): """Direct translation of FORTRAN function""" return x** 2 + 2 *x + 1 def trapezoidal_imperative(): a = 0. b = 1. n = 1000 h = (b - a) / n self.n = n self._result = None def compute_trapezoidal(self): h = (self.b - self.a) / self.n total = (self.function(self.a) + self.function(self.b)) / 2. for i in range( 1 , self.n): x = self.a + i * h total += self.function(x) self._result = total * h return self._result @property def result(self): if self._result is None: self.compute_trapezoidal() return self._result class QuadraticFunction: def __call__(self, x): return x** 2 + 2 *x + 1 _# Usage_ integrator = NumericalIntegrator(QuadraticFunction(), 0.0, 1.0, 1000 ) print(f'Integral = {integrator.compute_trapezoidal()}') **7.2 Example 2: Matrix Operations FORTRAN Source:** fortran SUBROUTINE matrix_multiply(A, B, C, n) IMPLICIT NONE INTEGER :: n, i, j, k REAL :: A(n,n), B(n,n), C(n,n), temp DO i = 1 , n DO j = 1 , n temp = 0. DO k = 1 , n temp = temp + A(i,k) * B(k,j) END DO C(i,j) = temp END DO END DO END SUBROUTINE **Python Conversion (NumPy - Recommended):** python import numpy as np def matrix_multiply_numpy(A, B): """ Leverages NumPy's optimized C implementation Significantly faster than manual loops """ return np.dot(A, B) _# or A @ B # For exact FORTRAN behavior (column-major order)_ def matrix_multiply_fortran_style(A, B): """Preserves FORTRAN's column-major ordering""" A_f = np.asfortranarray(A) B_f = np.asfortranarray(B) return np.dot(A_f, B_f) **Python Conversion (Manual - Educational):** python def matrix_multiply_manual(A, B): """ Direct translation of FORTRAN triple loop Demonstrates understanding of algorithm """